读取文件时,为什么我的输出不符合预期?

问题描述 投票:0回答:2

我正在阅读一个文件,其中包含格式为“Grid-ref= x, y”的几行,其中 x 和 y 旨在拆分并分配给 2 个变量 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

x 从 1 开始,每次运行该行时递增。它是 11x - 6 的原因是因为 gridref 行在文件中每 11 行出现一次,所以它总是在 11x - 6 行,其中 x 是一个整数,其余行是数据。这是文件的前 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”的第一个实例,但第二次出现这种情况时,这行代码似乎只存储了最后 2 位数字。所以对于“Grid-ref= 1, 311”,它返回单个值“11”而不是 1 和 311。有人能看出这是为什么吗?这行代码显然适用于第一个值,我不明白为什么它会给出错误的结果。任何帮助将不胜感激

我已经放了打印语句以确保它确实在正确的行上并且是这种情况

python split readfile
2个回答
1
投票

我假设你有类似的东西

with open("input.txt", "r") as input:
    lines = input.readlines()

在您发布的代码段上方。这意味着在您的循环中,

line
变量将在每次迭代中仅保留一行,因此
line[11*x-6]
将是该行从位置
11*x - 6
开始的子字符串,这意味着您在每行中砍掉越来越多的行,然后阅读它,随着
x
的增加。从你所在的位置到解决方案的最短路径 对于这个特定的输入 可能会改变

      xref, yref = line[11*x - 6:].replace("Grid-", "").replace("ref=", "").replace(" ", "").strip().split(',')

      xref, yref = line.replace("Grid-", "").replace("ref=", "").replace(" ", "").strip().split(',')

和 outdenting

l = l + 1
if
声明处于同一水平。但是,我建议您通读一下代码以简化它。例如,合并替换(您可以在一次替换中获得所有
Grid-ref=
)或用正则表达式搜索替换所有内容。


0
投票

您的代码需要每 22 行采样一次并查看

line
变量,并使用
l
作为行计数器:

...
l = 5
x = 1
for line in lines[5:]:
   if l == ((22*x) - 17):
      xref, yref = line.replace("Grid-", "").replace("ref=", "").replace(" ", "").strip().split(',')
      print(xref, yref)   # example usage
      x = x + 1
   l = l + 1

输出:

1 148
1 311
1 312

好的,现在您已经用真实文件内容更新了问题,我可以返回相隔 11 行的数据。

此外,您应该使用

enumerate()
为您计算行数:

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

相同的输出

© www.soinside.com 2019 - 2024. All rights reserved.