Python替换文本文件中的部分文本

时间:2016-11-16 16:10:59

标签: python text replace

当前文字文件:

NAME  S1  2581751 2581758       1            
NAME  S2  2581761 2581776       1               
NAME  S3  2581779 2581786       1      
NAME  S4  2581789 2581804       1        
NAME  S5 2581807 2581814       1           
C       A0-ABC          
    A(1) =   0.1518     !k2            
    B(2) =   0.4910     !K2O         
    C(3) =   0.0314     !D2       
    D(4) =   0.0876     !CO          
    E(5) =   0.2382     !C1        
C   CI-ABC                 
NAME  S5  2581779 2581786       1            
NAME  S5  2581789 2581804       1          
NAME  S5  2581807 2581814       1  

更新的文本文件如下所示:

NAME  S1  2581751 2581758       1            
NAME  S2  2581761 2581776       1               
NAME  S3  2581779 2581786       1      
NAME  S4  2581789 2581804       1        
NAME  S5 2581807 2581814       1           
C       A0-ABC          
    A(1) =   0.2145     !k2            
    B(2) =   0.542    !K2O         
    C(3) =   0.35     !D2       
    D(4) =   0.0476     !CO          
    E(5) =   0.123     !C1        
C   CI-ABC                 
NAME  S5  2581779 2581786       1            
NAME  S5  2581789 2581804       1          
NAME  S5  2581807 2581814       1  

我想将文本文件(0.15180.4910 ...)中的号码更改为新号码(0.21450.5242 ...),其中显示在下面。最初,我取出C A0-ABCC CI-ABC之间的界限并保存在列表中,然后更新数字并将其另存为不同的名称。但我有问题将更新的号码替换为原始文本文件。

import sys
import os
import numpy as np             
import math          
import string 

path='Directory..'                                                              
k2 =0.2145
K2O=0.542
D2 = 0.35
CO = 0.476
C1 = 0.123
Update=[str(k2),str(K2O),str(D2),str(CO),str(C1)]
files = os.listdir('.')
f = open(path+'\Input.txt','r')
lines = f.readlines()
copy = False
save_list=[]
for line in lines :
    if line.strip() == "C  A0-ABC":
       copy = True
    elif line.strip() == "C CI-ABC":
       copy = False
    elif copy:
       save_list.append(line)

 file= open(path+'\Update_INPF.txt','w')
 lis=[x.split() for x in save_list]
 A = [str(x[0]) for x in lis]
 B = [str(x[1]) for x in lis]
 C = Update
 D= [str(x[3]) for x in lis]
 F=zip(A,B,C,D)

 for x in list(F):
     file.write("{0}\t{1}\t{2}\t{3}\n".format(*x))
 file.close()
 print save_list

1 个答案:

答案 0 :(得分:0)

path = 'Directory..'                                                              

k2 = 0.2145
K2O = 0.542
D2 = 0.35
CO = 0.476
C1 = 0.123

fold = open(path + '\Input.txt','r')
fnew = open(path + '\Update_INPF.txt','w')

for line in f.readlines():

    line = line.split()

    if line[0] == 'A(1)':
        newline =  ' '.join(line[:2] + [str[k2]] + line[-1])
    elif line[0] == 'B(2)':
        newline =  ' '.join(line[:2] + [str[K20]] + line[-1])
    elif line[0] == 'C(3)':
        newline =  ' '.join(line[:2] + [str[D2]] + line[-1])
    elif line[0] == 'D(4)':
        newline =  ' '.join(line[:2] + [str[C0]] + line[-1])
    elif line[0] == 'E(5)':
        newline =  ' '.join(line[:2] + [str[C1]] + line[-1])
    else:
        newline = line

    fnew.write(newline)

fold.close()
fnew.close()