我如何从文件创建列表列表,其中每个列表都不止一行?

How can i create list of lists from file where every list would have more than one line?

我有这个大文件,

E01 2019 07 07 23 50 00-6.423263694160e-04-8.043343768804e-12 0.000000000000e+00
     1.500000000000e+01-8.437500000000e+00 2.970480875309e-09-8.238142721997e-02
    -3.967434167862e-07 1.850683474913e-04 6.241723895073e-06 5.440598007202e+03
     8.580000000000e+04 7.823109626770e-08 1.189137422632e-01 6.519258022308e-08
     9.876445222158e-01 2.184375000000e+02-3.055987605922e+00-5.639163465222e-09
    -4.485901141503e-10 5.170000000000e+02 2.061000000000e+03 0.000000000000e+00
     3.120000000000e+00 0.000000000000e+00-1.862645149231e-09-2.328306436539e-09
     9.999000000000e+08                                                         
E01 2019 07 07 23 50 00-6.423254380934e-04-8.043343768804e-12 0.000000000000e+00
     1.500000000000e+01-8.437500000000e+00 2.970480875309e-09-8.238142721997e-02
    -3.967434167862e-07 1.850683474913e-04 6.241723895073e-06 5.440598007202e+03
     8.580000000000e+04 7.823109626770e-08 1.189137422632e-01 6.519258022308e-08
     9.876445222158e-01 2.184375000000e+02-3.055987605922e+00-5.639163465222e-09
    -4.485901141503e-10 2.580000000000e+02 2.061000000000e+03 0.000000000000e+00
     3.120000000000e+00 0.000000000000e+00-1.862645149231e-09 0.000000000000e+00
     9.999000000000e+08...

并且我需要创建列表列表,其中每个列表都从 'E01' 开始并以下一个 "E01" 开始之前的最后一个数字结束,像这样

[E01, 2019, 07, ... 2.328306436539e-09, 9.999000000000e+08]

我只知道如何将每一行放入单独的列表 (.readlines()),但是我的文件对于这种方法来说太大了。

直截了当:

with open('yourfile.txt') as f:
    res = []
    for line in f:
        if line.startswith('E'):
            res.append([])
        res[-1].extend(line.split())

print(res)

详情:

  • line.split() - 按空白字符将行拆分为字符串列表
  • res[-1].extend(line.split()) - 使用拆分结果(上述步骤)扩展(父列表的)最后一个子列表

示例输出(2 个部分):

[['E01', '2019', '07', '07', '23', '50', '00-6.423263694160e-04-8.043343768804e-12', '0.000000000000e+00', '1.500000000000e+01-8.437500000000e+00', '2.970480875309e-09-8.238142721997e-02', '-3.967434167862e-07', '1.850683474913e-04', '6.241723895073e-06', '5.440598007202e+03', '8.580000000000e+04', '7.823109626770e-08', '1.189137422632e-01', '6.519258022308e-08', '9.876445222158e-01', '2.184375000000e+02-3.055987605922e+00-5.639163465222e-09', '-4.485901141503e-10', '5.170000000000e+02', '2.061000000000e+03', '0.000000000000e+00', '3.120000000000e+00', '0.000000000000e+00-1.862645149231e-09-2.328306436539e-09', '9.999000000000e+08'],
 ['E01', '2019', '07', '07', '23', '50', '00-6.423254380934e-04-8.043343768804e-12', '0.000000000000e+00', '1.500000000000e+01-8.437500000000e+00', '2.970480875309e-09-8.238142721997e-02', '-3.967434167862e-07', '1.850683474913e-04', '6.241723895073e-06', '5.440598007202e+03', '8.580000000000e+04', '7.823109626770e-08', '1.189137422632e-01', '6.519258022308e-08', '9.876445222158e-01', '2.184375000000e+02-3.055987605922e+00-5.639163465222e-09', '-4.485901141503e-10', '2.580000000000e+02', '2.061000000000e+03', '0.000000000000e+00', '3.120000000000e+00', '0.000000000000e+00-1.862645149231e-09', '0.000000000000e+00', '9.999000000000e+08']]