简体   繁体   English

从文件中读取多行列表

[英]read multi-line list from file

I have a file with data like: 我有一个数据如下的文件:

POTENTIAL
TYPE               1
 -5.19998150116627E+07 -5.09571848744513E+07 -4.99354600752570E+07 -4.89342214499422E+07 -4.79530582388520E+07
 -4.69915679183017E+07 -4.60493560354389E+07 -4.51260360464197E+07 -4.42212291578282E+07 -4.33345641712756E+07
 -4.24656773311163E+07 -4.16142121752159E+07 -4.07798193887125E+07 -3.99621566607090E+07 -3.91608885438409E+07
 -3.83756863166569E+07
 -8.99995987594328E+07 -8.81884626368405E+07 -8.64137733336537E+07 -8.46747974037847E+07 -8.29708161608188E+07
 -8.13011253809965E+07 -7.96650350121689E+07 -7.80618688886128E+07 -7.64909644515842E+07 -7.49516724754953E+07
 -7.34433567996002E+07 -7.19653940650832E+07 -7.05171734574350E+07 -6.90980964540154E+07 -6.77075765766936E+07
 -6.63450391494693E+07 

Note as per Nsh's comment these data are not single line. 请注意,根据Nsh的评论,这些数据不是一行。 They always have 5 data per line, and as per this example, 4 row, with only one data in 4th row. 它们始终每行有5个数据,并且在本示例中为4行,第4行只有一个数据。 So, I have 16 float spread over 4 line. 因此,我有16个浮点数分布在4行上。 I always know the total number (ie 16 in this case) 我总是知道总数(在这种情况下为16)

My aim is to read them as a list (please let me know if there is better things). 我的目的是将它们作为列表阅读(请告诉我是否有更好的东西)。 The row with the single entry denotes end of a list (eg the list[1] ends with -3.83756863166569E+07 ). 具有单个条目的行表示列表的结尾(例如list [1]以-3.83756863166569E+07结尾)。

I tried to read it as: 我试图将其读取为:

    if line.startswith("POTENTIAL"):
        lines = f.readline()
        if lines.startswith("TYPE  "):
            lines=f.readline()
            lines=lines.split()
            lines = [float(i) for i in lines]
            pots.append(lines)
        print(pots)

which gives result: 结果如下:

[[-51999815.0116627, -50957184.8744513, -49935460.075257, -48934221.4499422, -47953058.238852]]

ie just the first line from the list, and not going any further. 即只是列表中的第一行,并且不再进行任何其他操作。 My aim is to get them as different list (possibly) as: 我的目标是将它们作为不同的列表(可能)作为:

pots[1]=[-5.19998150116627E+07....-3.83756863166569E+07]
pots[2]=[-8.99995987594328E+07....-6.63450391494693E+07]

I have read searched google extensively (the present state itself is from another SO question), but due to my inexperience, I cant solve my problem. 我已经广泛地搜索过google(当前状态本身是来自另一个SO问题),但是由于我的经验不足,我无法解决我的问题。

Kindly help. 请帮助。

use + instead of append . +代替append
It will append the elements of lines to pots. 它将线条元素添加到花盆中。
pots = pots + lines

I didn't see in the start: 一开始我没看到:
pots = []

It is needed in this case... 在这种情况下是需要的...

ITEMS_PER_LIST = 16
lists = [[]] # list of lists with initialized first sublist
with open('data.txt') as f:
    for line in f:
        if line.startswith(("POTENTIAL", "TYPE")):
            continue

        if len(lists[-1]) == ITEMS_PER_LIST:
            lists.append([])  # create new list

        lists[-1].extend([float(i) for i in line.split()])

Additional tweaks are required to validate headers. 需要其他调整来验证标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM