如何以特定顺序写出文件?

时间:2017-07-31 15:43:42

标签: python

我想从一个文件中读取," ee_pinkH1.xpk" 这就是我的ee_pinkH1.xpk文件:

label dataset sw sf
1H 1H_2
NOESY_F1eF2e.nv
4807.69238281 4803.07373047
600.402832031 600.402832031
1H.L 1H.P 1H.W 1H.B 1H.E 1H.J 1H.U 1H_2.L 1H_2.P 1H_2.W 1H_2.B 1H_2.E 1H_2.J 1H_2.U vol int stat comment flag0 flag8 flag9
0 {} 7.78881 0.02842 0.04763 ++ {0.0} {} {} 5.89277 0.07262 0.11966 ++ {0.0} {} 0.0 7.3981 0 {} 0 0 0
1 {} 7.61431 0.04366 0.07181 ++ {0.0} {} {} 5.87817 0.06830 0.11334 ++ {0.0} {} 0.0 5.5413 0 {} 0 0 0
2 {} 8.05367 0.02879 0.03707 ++ {0.0} {} {} 5.73575 0.07116 0.10207 ++ {0.0} {} 0.0 4.4803 0 {} 0 0 0
3 {} 7.16881 0.03259 0.04929 ++ {0.0} {} {} 5.73917 0.06553 0.10821 ++ {0.0} {} 0.0 5.4038 0 {} 0 0 0
4 {} 7.94518 0.02852 0.04924 ++ {0.0} {} {} 5.63447 0.12494 0.10869 ++ {0.0} {} 0.0 2.4702 0 {} 0 0 0
5 {} 7.65731 0.05419 0.07742 ++ {0.0} {} {} 5.65039 0.08312 0.12708 ++ {0.0} {} 0.0 4.3950 0 {} 0 0 0
6 {} 7.60002 0.04926 0.07038 ++ {0.0} {} {} 5.63917 0.13377 0.24062 ++ {0.0} {} 0.0 6.3350 0 {} 0 0 0
7 {} 7.53822 0.03728 0.08602 ++ {0.0} {} {} 5.64647 0.12031 0.17187 ++ {0.0} {} 0.0 11.4796 0 {} 0 0 0
8 {} 7.49302 0.03011 0.04301 ++ {0.0} {} {} 5.64581 0.10163 0.14260 ++ {0.0} {} 0.0 4.2979 0 {} 0 0 0
9 {} 7.38454 0.02958 0.04024 ++ {0.0} {} {} 5.64981 0.06854 0.10766 ++ {0.0} {} 0.0 4.7322 0 {} 0 0 0
10 {} 8.04667 0.03795 0.05967 ++ {0.0} {} {} 5.60118 0.07613 0.11373 ++ {0.0} {} 0.0 5.2641 0 {} 0 0 0
11 {} 7.89060 0.04859 0.05923 ++ {0.0} {} {} 5.60680 0.09108 0.10902 ++ {0.0} {} 0.0 3.9888 0 {} 0 0 0
12 {} 7.38360 0.03251 0.04058 ++ {0.0} {} {} 5.49793 0.05913 0.07784 ++ {0.0} {} 0.0 3.7279 0 {} 0 0 0
13 {} 7.66280 0.02544 0.06748 ++ {0.0} {} {} 5.48500 0.08846 0.11309 ++ {0.0} {} 0.0 3.9823 0 {} 0 0 0
14 {} 7.60046 0.06299 0.09462 ++ {0.0} {} {} 5.48296 0.09213 0.13565 ++ {0.0} {} 0.0 5.1588 0 {} 0 0 0
15 {} 7.30477 0.03561 0.03189 ++ {0.0} {} {} 5.45582 0.07882 0.06792 ++ {0.0} {} 0.0 2.5031 0 {} 0 0 0
16 {} 7.69287 0.04291 0.05129 ++ {0.0} {} {} 5.41687 0.09768 0.11328 ++ {0.0} {} 0.0 3.6440 0 {} 0 0 0
17 {} 7.53968 0.05164 0.09332 ++ {0.0} {} {} 5.38349 0.11427 0.19768 ++ {0.0} {} 0.0 7.4048 0 {} 0 0 0
18 {} 8.06684 0.02564 0.02865 ++ {0.0} {} {} 5.16953 0.10655 0.11644 ++ {0.0} {} 0.0 3.2906 0 {} 0 0 0
19 {} 7.44620 0.03758 0.00001 ++ {0.0} {} {} 5.18077 0.17121 0.00003 ++ {0.0} {} 0.0 1.4552 0 {} 0 0 0

我想从每一行中取出特定值并将其写出另一个名为" tclust.txt"的文件。目前,这是我的文件输出的样子:

rbclust
Peak 2  7.61431 0.05  5.87817 0.05
Peak 3  8.05367 0.05  5.73575 0.05
Peak 4  7.16881 0.05  5.73917 0.05
Peak 5  7.94518 0.05  5.63447 0.05
Peak 6  7.65731 0.05  5.65039 0.05
Peak 7  7.60002 0.05  5.63917 0.05
Peak 8  7.53822 0.05  5.64647 0.05
Peak 9  7.49302 0.05  5.64581 0.05
Peak 10  7.38454 0.05  5.64981 0.05
Peak 11  8.04667 0.05  5.60118 0.05
Peak 12  7.89060 0.05  5.60680 0.05
Peak 13  7.38360 0.05  5.49793 0.05
Peak 14  7.66280 0.05  5.48500 0.05
Peak 15  7.60046 0.05  5.48296 0.05
Peak 16  7.30477 0.05  5.45582 0.05
Peak 17  7.69287 0.05  5.41687 0.05
Peak 18  7.53968 0.05  5.38349 0.05
Peak 19  8.06684 0.05  5.16953 0.05
Peak 20  7.44620 0.05  5.18077 0.05
Peak 1  7.78881 0.05  5.89277 0.05

这是我的代码:

import os
import sys
import re

i = 0;
contents_peak = []
peak_lines=[]
with open ("ee_pinkH1.xpk","r") as peakPPM:
    for PPM in peakPPM.readlines():
        float_num = re.findall("[\s][1-9]{1}\.[0-9]+",PPM)
        if (len(float_num)>1):
            i=i+1
            value = ('Peak ' + str(i) + ' '+  str(float_num[0])+ ' 0.05'+ '\n')
            peak_lines.insert(-1,value)
tclust_peak = open("tclust.txt","w+")
tclust_peak.write('rbclust \n')
for value in peak_lines:
    tclust_peak.write(value)
tclust_peak.close()

j = 0;
contents_atom = []
atom_lines=[]
with open ("peaks_ee.xpk","r") as atomName:
   for name in atomName.readlines():
        float_str = re.findall("\d\.H\d'?", name)
        if (len(float_str)>1):
            j = j+1
            value1 = ('Atom ' + str(j) + ' ' + str(float_str[0]) + ' ' + str(float_str[1]) + '\n')
             atom_lines.insert(-1,value1)

 tclust_atom = open("tclust.txt","a")
 for value1 in atom_lines:
     tclust_atom.write(value1)
 tclust_atom.close()

现在,第一行从峰值2开始,以峰值1结束,如何从峰值1开始?

2 个答案:

答案 0 :(得分:6)

...在最后添加条目,而不是在最后一个条目之前插入它们。

peak_lines.append(value)

答案 1 :(得分:1)

是的......或者你只是把它写在循环中。看这个: How can I open multiple files using "with open" in Python?

with open('a', 'w') as a, open('b', 'w') as b:
    do_something()

所以:

with open ("ee_pinkH1.xpk","r") as peakPPM, open("tclust.txt","w+") as tclust_peak:

#Loop...
   #Code here....
   tclust_peak.write('rbclust \n')
   value = "Peak {} {} 0.05".format(str(i), str(float_num[0])
   tclust_peak.write(value)