的线我已经从一个文本文件中的以下输入:Python的 - 解析文本
Title Value Position Perturbation 1.5 0.6 8.5 9.8 0 8.5 9.6 0.5 0.6 (...)
Title Value Position Perturbation 3 1.5 6 0 0.8 9.7 5.3 9.9 0.7 0.9 (...)
我想删除前4列,与数列我想子集,每4个价值观和改变第3个值的对于第二个和所述位置移除第四之一,因此,输出应为:
1.5 8.5 0.6 0 9.6 8.5 0.6 (...)
3 6 1.5 0.8 5.3 9.7 0.7 (...)
对于此提出我编写以下Python代码:
import sys
input_file= open (sys.argv[1],'r')
output_file= open (sys.argv[2], 'w')
with open(sys.argv[1]) as input_file:
for i, line in enumerate(input_file):
output_file.write ('\n')
marker_info= line.split()
#snp= marker_info[0]
end= len(marker_info)
x=4
y=8
# while y<=len(marker_info):
while x<=end:
intensities= marker_info[x:y]
AA= intensities[0]
BB= intensities[1]
AB= intensities[2]
NN= intensities[3]
output_file.write ('%s' '\t' '%s' '\t' '%s' '\t' % (AA, AB, BB))
x= y
y= x + 4
input_file.close()
output_file.close()
该代码似乎工作正常,但问题是,对于每一行,最后四个值都丢失。所以,我猜这个问题出现在“while”语句中......但我不知道如何解决它(我知道这似乎是一个简单的问题)。
在此先感谢您的任何建议。
仅供参考:当使用资源你不需要手动关闭输入流。这就是为什么存在“with”的原因:-) – oopbase 2014-10-08 08:05:59