I am reading in a file that contains several lines that are of the format ‘Grid-ref= x, y’ where x and y is meant to be split and asigned to 2 variables xref and yref. I use the following code to do this
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 starts from 1 and increments every time this line is run. the reason it is 11x – 6 is because the gridref line comes every 11 lines in the file, so it is always at line 11x – 6 where x is an integer, the rest of the lines are data. here is the first 30 lines of the file just to give more context
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
so for example, ‘Grid-ref= 1, 311’ should assign 1 and 311 to xref and yref respectively. This seems to work for the first instance of ‘Grid-ref’, but then the second time this occurs, this line of code seems to result in only the last 2 digits being stored. so for ‘Grid-ref= 1, 311′ it returns a single value ’11’ rather than 1 and 311. can anyone see why that is? This line of code obviously works as it works for the first value, and i can’t see why it is giving a wrong result. any help would be much appreciated
I have put print statements to ensure that it is indeed on the correct line and this is the case
>Solution :
Your code needs to sample every 22 lines and look into the line variable, and use l as the line counter:
...
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
Output:
1 148
1 311
1 312
Ok, so now that you’ve updated the question with the real file contents, I can go back to data 11 lines apart.
Also, you should use enumerate() to count the lines for you:
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
Same output