数据块在文件中的顺序相反:
C5401D8E17292151102G41+0NN003EXE00812
+0000.,+13.53,+24.39,+23.15,+09.02,+1010.,+254.9,+21.32,-8190.,+0.039,+17.58,+263.2,+10.00,+0.000,+0.000,+5.442,+237.7,+0.000,+764.1,
+0000.,+13.53,+24.27,+23.06,+08.99,+0989.,+248.5,+21.28,-8190.,+0.039,+17.58,+263.1,+10.00,+0.000,+0.000,+3.381,+233.1,+0.000,+764.2,
+0000.,+13.54,+24.08,+23.09,+08.99,+0966.,+241.9,+21.27,-8190.,+0.039,+17.58,+263.0,+10.00,+0.000,+0.000,+3.754,+248.9,+0.000,+764.2,
+0000.,+13.53,+23.89,+23.06,+09.05,+0942.,+235.0,+21.25,-8190.,+0.040,+17.61,+262.9,+10.00,+0.000,+0.000,+2.778,+243.7,+0.000,+764.2,
+0000.,+13.55,+23.82,+22.40,+08.71,+0917.,+227.9,+21.25,+37.73,+0.039,+17.59,+263.4,+10.00,+0.000,+0.000,+2.995,+225.3,+0.000,+764.2,
+0000.,+13.56,+23.85,+22.30,+09.12,+0890.,+220.6,+21.26,+36.02,+0.039,+17.58,+263.2,+10.00,+0.000,+0.000,+4.331,+229.0,+0.000,+764.2,
C5401D8E17292141102G43+0NN003EXE00812
+0000.,+13.56,+23.71,+22.74,+08.24,+0860.,+212.6,+21.27,+34.62,+0.039,+17.58,+263.1,+10.00,+0.000,+0.000,+3.533,+242.0,+0.000,+764.3,
+0000.,+13.58,+23.33,+23.45,+7.624,+0830.,+205.1,+21.30,+33.98,+0.039,+17.59,+262.5,+10.00,+0.000,+0.000,+2.013,+259.4,+0.000,+764.3,
+0000.,+13.60,+22.78,+23.19,+7.420,+798.0,+196.7,+21.33,+33.06,+0.039,+17.58,+263.4,+10.00,+0.000,+0.000,+1.458,+258.2,+0.000,+764.3,
+0000.,+13.61,+22.25,+22.50,+08.58,+763.0,+187.7,+21.37,+31.70,+0.040,+17.61,+263.9,+10.00,+0.000,+0.000,+1.567,+328.9,+0.000,+764.2,
+0000.,+13.64,+21.68,+22.40,+08.17,+727.5,+179.3,+21.42,+30.47,+0.039,+17.59,+263.3,+10.00,+0.000,+0.000,+2.519,+255.3,+0.000,+764.1,
+0000.,+13.64,+21.09,+21.80,+09.22,+692.0,+170.4,+21.47,+29.13,+0.039,+17.59,+262.8,+10.00,+0.000,+0.000,+1.991,+356.7,+0.000,+764.0,
C5401D8E17292131102G42+0NN003EXE00812
+0000.,+13.67,+20.50,+21.50,+08.34,+654.7,+161.2,+21.52,+27.72,+0.039,+17.59,+262.9,+10.00,+0.000,+0.000,+1.795,+12.49,+0.000,+764.0,
+0000.,+13.67,+19.94,+21.16,+08.27,+616.0,+151.8,+21.58,+26.17,+0.039,+17.59,+263.2,+10.00,+0.000,+0.000,+1.694,+31.86,+0.000,+763.9,
+0000.,+13.71,+19.39,+21.06,+08.51,+575.8,+142.2,+21.65,+24.47,+0.039,+17.59,+263.8,+10.00,+0.000,+0.000,+1.807,+46.90,+0.000,+763.9,
+0000.,+13.53,+18.83,+20.91,+7.692,+535.2,+132.3,+21.71,+22.74,+0.040,+17.62,+264.0,+10.00,+0.000,+0.000,+2.762,+54.26,+0.000,+763.8,
+0000.,+13.49,+18.27,+20.73,+5.718,+493.4,+121.7,+21.78,+21.21,+0.040,+17.60,+264.1,+10.00,+0.000,+0.000,+3.138,+66.29,+0.000,+763.7,
+0000.,+13.72,+17.71,+20.19,+5.038,+451.0,+110.8,+21.84,+19.71,+0.040,+17.59,+264.5,+10.00,+0.000,+0.000,+3.479,+75.01,+0.000,+763.6,
第一行将日期[8:15]保留为年份,日期,小时。因此:
timestamp= datetime.datetime.strptime (value, '%y%j%H' )
如何逐行读出并按日期和附加的数据行获取可排序列表或词典?
亲切的问候。
答案 0 :(得分:0)
我假设一条空行将一个街区与另一个街区分开。随意在for循环内的第一行更改该条件。
blocks, currentBlock = [], []
for line in lines:
if line=='' and currentBlock:
blocks.append(currentBlock)
currentBlock = []
else:
currentBlock.append(line)
# if last line was not an empty, currentBlock is not empty, and must be appended:
if currentBlock:
blocks.append(currentBlock)
这将为您提供一系列行数组:
[
['C5401D8E17292151102G41+0NN003EXE00812 ', '+0000.,+13.53,+24.39,+23.15,+09.02,+1010.,+254.9,+21.32,-8190.,+0.039,+17.58,+263.2,+10.00,+0.000,+0.000,+5.442,+237.7,+0.000,+764.1,', ...],
['C5401D8E17292141102G43+0NN003EXE00812 ', ...
要对这些块进行排序,请使用list.sort
并提供一个关键功能来提取排序键(日期,包含在字符8:15的第一行):
blocks.sort(key=lambda lines: datetime.datetime.strptime(lines[0][8:15],'%y%j%H'))
现在您可以使用相同的格式打印块:
for block in blocks:
for line in block:
print(line)
print('')