我创建了一个python脚本,它接受一个包含大量列的文件,并提取并复制(复制到新文件中)我需要的两列。但是,每月一次或两次我需要在同一个(新)文件中添加其他列,但我需要将它们添加到当前列的一侧,而不是底部。如何修改脚本来做到这一点?
这些是输入文件的一些示例。
https://dl.dropbox.com/u/63216126/file5.txt
https://dl.dropbox.com/u/63216126/file6.txt
这是我写的代码。
import sys
import re
filetoread = sys.argv[1]
filetowrite = sys.argv[2]
newfile = str(filetowrite) + ".txt"
openold = open(filetoread,"r")
opennew = open(newfile,"a")
rline = openold.readlines()
number = int(len(rline))
start = 0
for i in range (len(rline)) :
if "2theta" in rline[i] :
start = i
for line in rline[start + 1 : number] :
words = line.split()
word1 = words[1]
word2 = words[2]
opennew.write (word1 + " " + word2 + "\n")
openold.close()
opennew.close()