2014-10-08 66 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