You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

读取文件提取Grid-ref坐标值时出现异常的问题求助

读取文件提取Grid-ref坐标值时出现异常的问题求助

我现在在处理一个文本文件,里面有若干行格式为Grid-ref= x, y的内容,需要把x和y拆分出来分别赋值给xref和yref两个变量。我写了下面这段代码来实现:

l = 5
x = 1

for line in lines[5:]:
    if l == ((11*x) - 6):
        xref, yref = line[11*x - 6:].replace("Grid-", "").replace("ref=", "").replace(" ", "").strip().split(',')
        x = x + 1
    l = l + 1
    continue

我的思路是,Grid-ref行每隔11行出现一次,所以它的位置应该是11*x - 6(x从1开始递增),其余行都是数据内容。

下面是文件的前30行,给大家提供上下文参考:

Tyndall Centre grim file created on 22.01.2004 at 17:57 by Dr. Tim Mitchell

.pre = precipitation (mm)

CRU TS 2.1

[Long=-180.00, 180.00] [Lati= -90.00,  90.00] [Grid X,Y= 720, 360]

[Boxes=   67420] [Years=1991-2000] [Multi=    0.1000] [Missing=-999]

Grid-ref=   1, 148

3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630

3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630

3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630

3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630

3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630

3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630

3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630

3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630

3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630

3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630

Grid-ref=   1, 311

490  290  280  230  200  250  440  530  460  420  530  450

490  290  280  230  200  250  440  530  460  420  530  450

490  290  280  230  200  250  440  530  460  420  530  450

490  290  280  230  200  250  440  530  460  420  530  450

490  290  280  230  200  250  440  530  460  420  530  450

490  290  280  230  200  250  440  530  460  420  530  450

490  290  280  230  200  250  440  530  460  420  530  450

490  290  280  230  200  250  440  530  460  420  530  450

490  290  280  230  200  250  440  530  460  420  530  450

490  290  280  230  200  250  440  530  460  420  530  450

Grid-ref=   1, 312

460  280  260  220  190  240  430  520  450  400  520  410

460  280  260  220  190  240  430  520  450  400  520  410

按道理来说,比如遇到Grid-ref= 1, 311时,应该把1和311分别赋值给xref和yref。现在的情况是,第一次出现Grid-ref行时代码能正常工作,但第二次遇到的时候,提取出来的结果不对——比如处理Grid-ref= 1, 311时,得到的不是1和311,而是只有11这个值。

我已经加了print语句确认代码确实定位到了正确的行,但就是提取结果出错,实在找不到问题出在哪,有没有大佬能帮忙看看呀?

备注:内容来源于stack exchange,提问作者Mohammed Yakub

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 16:03:03