2014-10-08 62 views
0

的线我已经从一个文本文件中的以下输入: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”语句中......但我不知道如何解决它(我知道这似乎是一个简单的问题)。

在此先感谢您的任何建议。

+2

仅供参考:当使用资源你不需要手动关闭输入流。这就是为什么存在“with”的原因:-) – oopbase 2014-10-08 08:05:59

回答

0

试试这一个,其所有基于脚本,除了在同时表达和公开文件的方法。 输入文件:

Title Value Position Perturbation 1.5 0.6 8.5 9.8 0 8.5 9.6 0.5 0.6 1.1 2.2 3.3 
Title Value Position Perturbation 3 1.5 6 0 0.8 9.7 5.3 9.9 0.7 0.9 1.1 2.2 
Title Value Position Perturbation 3.1 2.5 1.6 0 1.8 2.7 4.3 6.9 3.7 1.9 2.1 3.2 

脚本:

with open("parser.txt", "r") as input_file, open("output_parser.txt","w") as output_file: 
    for i, line in enumerate(input_file): 
     output_file.write ('\n') 
     marker_info= line.split() 
     end= len(marker_info) 
     x=4 
     y=8 

     while y<=end: #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)) 
      print end, x, y, marker_info[x:y], AA, AB, BB 

      x= y 
      y= x + 4 

输出:

1.5 8.5 0.6 0 9.6 8.5 0.6 2.2 1.1 
3 6 1.5 0.8 5.3 9.7 0.7 1.1 0.9 
3.1 1.6 2.5 1.8 4.3 2.7 3.7 2.1 1.9 
2

尝试此:
1.如CSV打开文件和剥离标签
2.生成期望大小
3的子列表做你交换和删除尾随元件
4.保存输出(I受够了名单做了,但你可以输出文件做)

>>> import csv 
>>> output = [] 
>>> with open('sample.csv') as input: 
...  reader = csv.reader(input, delimiter=' ') 
...  for line in reader: 
...   line = line[4:] #strip labels 
...   slice_size = 4 
...   for slice_idx in range(0,len(line),slice_size): 
...    sublist = line[slice_idx : slice_idx+slice_size] 
...    if len(sublist) == slice_size: 
...     swap = sublist[2] 
...     sublist[2] = sublist[1] 
...     sublist[1] = swap 
...     output.append(sublist[:slice_size-1]) 
... 
>>> 
>>> output 
[['1.5', '8.5', '0.6'], ['0', '9.6', '8.5'], ['3', '6', '1.5'], ['0.8', '5.3', '9.7']] 
+0

slice_size = 4 ^ IndentationError:unindent不匹配任何外部缩进级别 – user2245731 2014-10-08 08:34:41

+0

脚本缩进很多,您可以执行复制粘贴错误吗? – xecgr 2014-10-08 08:37:36

+0

我不确定你的意思,我得到以前的错误,但没有更多的信息。无论如何,你有任何线索错误是在我的代码中?只是为了了解错误在哪里,并能够提高我的编码技能。 – user2245731 2014-10-08 09:03:19