Justin Justin - 22 days ago 5
Python Question

How to separate list values in nested list

I have to read RINEX observation file and compare with satellite informations.
Until now, I read some data. So, I have a nested list. I have to separate this list values by comma and put 'None' in spaces. List is like this;

oblst = [[' 22527689.086 7 -578290.975 7 -410051.96348 22527682.94948\n',
' 20593906.672 7 -142315.266 7 -103992.19948 20593902.01648\n',
' 22312367.273 6 22312363.496 9 -573672.801 6 -426591.952 9 22312363.52047\n',
' 24649943.453 3 -696710.409 3 -499047.10745 24649943.55545\n',
' 21574180.359 7 -440212.809 7 -323130.81248 21574175.09448\n',
' 20159532.789 7 35621.205 7 26243.45049 20159527.23449\n',
' 24435415.719 6 24435413.602 9 417737.346 6 319304.620 9 24435413.80147\n',
' 23108319.461 5 344109.572 5 249072.03046 23108315.90646\n',
' 21789481.414 7 326180.505 7 238895.05548 21789474.12548\n',
' 21975436.727 7 277875.380 7 205734.181 8 21975443.121 8\n',
' 19981973.117 9 -127270.254 9 -94211.274 9 19981980.414 9\n',
' 21682862.406 6 242033.680 6 179454.101 8 21682869.020 8\n',
' 20647552.734 7 -446216.926 7 -329944.882 9 20647562.945 9\n',
' 23174371.141 6 -645644.090 6 -481288.202 8 23174379.656 8\n',
' 21946454.047 6 420271.088 6 311362.996 8 21946462.590 8\n',
' 21922170.063 8 -124733.763 8 -92183.249 9 21922177.883 9\n'],
[......],[......]]


I'd like to print like this;

oblst = [['22527689.086 7','None','-578290.975 7','-410051.96348','22527682.94948',
'20593906.672 7','None','-142315.266 7','-103992.19948','20593902.01648',
'22312367.273 6','22312363.496 9','-573672.801 6','-426591.952 9','22312363.52047',
......],[...]]


Finally, if I print oblst[0][1], I want to print like this;

'22527689.086 7'


I tried to this using for-loop. This is what I wrote code.

for lines in oblst:
result = []
for line in lines:
for k in range(0, len(line)-1, 16):
numbers = line[k:k+16].strip()
result.append(numbers if numbers else None)
print result


output was like this; it was not bad.

result = ['22527689.086 7', 'None', '-578290.975 7', '-410051.96348', '22527682.94948',
'20593906.672 7', 'None', '-142315.266 7', '-103992.19948', '20593902.01648',
'22312367.273 6', '22312363.496 9', '-573672.801 6', '-426591.952 9', '22312363.52047',
'24649943.453 3', 'None', '-696710.409 3',...][....]]


but I printed result[0]. It was first values each lists. Indexes set vertically.

22527689.086 7
22524473.070 7
22521257.078 7
22518041.602 7
22514825.805 7
22511610.484 7
.
.
.


I really appreciate that you may help or give any advice for me.
Please help or any advice for me.

Answer

How rigid is the structure? If it is very rigid then here's an example of converting the first item to a list meeting your requirement. However, this is very fragile:

>>> [s.strip() for s in '  None'.join(s.strip().split('                 ')).split('  ')]
['22527689.086 7', 'None', '-578290.975 7', '-410051.96348', '22527682.94948']

If you want to convert this back to a string with ,s then:

>>> ', '.join(s.strip() for s in '  None'.join(s.strip().split('                 ')).split('  '))
'22527689.086 7, None, -578290.975 7, -410051.96348, 22527682.94948'
Comments