【问题标题】:How to interate over N lines of a text file in Python如何在 Python 中迭代 N 行文本文件
【发布时间】:2016-11-02 17:50:20
【问题描述】:

您好,我正在尝试将文件 disp.txt 转换为:

116  C             0.12 -0.91  0.39    -0.40  0.31  0.85    -0.66 -0.18 -0.22
117  O             0.00 -0.02  0.00    -0.05  0.05  0.12    -0.57 -0.26 -0.29
116  C            -0.03 -0.04  0.00     0.01  0.09  0.19    -0.71 -0.21 -0.26
117  O            -0.14  0.88 -0.45     0.47 -0.33 -0.79     0.57  0.16  0.19

到 disp.mol:

vibration 1
0.12  -0.91  0.39
0.0  -0.02  0.0
vibration 2
-0.4  0.31  0.85
-0.05  0.05  0.12
vibration 3
-0.66  -0.18  -0.22
-0.57  -0.26  -0.29
vibration 4 
-0.03 -0.04 0.00 
-0.14 0.88 -0.45
vibration 5 ...

我使用 with open('disp.txt', 'w') as f: 打开了 disp.txt 文件:

使用lines = f.readlines()读取行

在行中使用 for x 分割数据:numbers = x.split()

那么 vib1.append(float(numbers[2])), vib2.append 并将它们存储在 vib1=[]、vib2=[]等

当我需要以 disp.mol 格式存储的数据时,我的问题就出现了。使用下面的代码,我可以从前两行获得前三个振动的输出,但我不确定如何在接下来的两行 2 上执行相同的循环(以及如果我有更多的 2N 行) .我也不确定如何给每个振动编号。对此的任何帮助将不胜感激。

with open('disp.mol', 'w') as thisfile:
        thisfile.writelines('vibration')
        thisfile.writelines('\n')
        for x in range (0, 2):
                vib_one = str(vib1[x]) + '  ' +  str(vib2[x]) + '  ' +  str(vib3[x])
                thisfile.writelines(vib_one)
                thisfile.writelines('\n')
        thisfile.writelines('vibration')
        thisfile.writelines('\n')
        for x in range (0, 2):
                vib_two = str(vib4[x]) + '  ' +  str(vib5[x]) + '  ' +  str(vib6[x])
                thisfile.writelines(vib_two)
                thisfile.writelines('\n')
        thisfile.writelines('vibration')
        thisfile.writelines('\n')
        for x in range (0, 2):
                vib_three = str(vib7[x]) + '  ' +  str(vib8[x]) + '  ' +  str(vib9[x])
                thisfile.writelines(vib_three)
                thisfile.writelines('\n')

输出:

vibration
0.12  -0.91  0.39
0.0  -0.02  0.0
vibration
-0.4  0.31  0.85
-0.05  0.05  0.12
vibration
-0.66  -0.18  -0.22
-0.57  -0.26  -0.29

【问题讨论】:

    标签: python-2.7 python-3.x parsing split multiple-columns


    【解决方案1】:

    这是一种方法:

    with open('disp.txt') as f, open('disp.mol','w') as out:
        vibration = 1
        for line in f:
            line1 = line.split()
            line2 = next(f).split() # also get next line
            for i in range(2,len(line1),3):
                out.write('vibration {}\n'.format(vibration))
                out.write(' '.join(line1[i:i+3])+'\n')
                out.write(' '.join(line2[i:i+3])+'\n')
                vibration += 1
    

    输出:

    vibration 1
    0.12 -0.91 0.39
    0.00 -0.02 0.00
    vibration 2
    -0.40 0.31 0.85
    -0.05 0.05 0.12
    vibration 3
    -0.66 -0.18 -0.22
    -0.57 -0.26 -0.29
    vibration 4
    -0.03 -0.04 0.00
    -0.14 0.88 -0.45
    vibration 5
    0.01 0.09 0.19
    0.47 -0.33 -0.79
    vibration 6
    -0.71 -0.21 -0.26
    0.57 0.16 0.19
    

    【讨论】:

    • 非常感谢,这很好。在您的解决方案中,我对 next(f).split() 的含义有些困惑。但我想我现在对它有了更好的理解。
    • @Tillie f 是输入文件迭代器。 next(f) 立即读取另一行,因此每次通过for 循环读取两行。 split() 只是将空白处的行分成一个列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 2018-12-08
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多