2014-10-08 2 views
0

의 라인 분석 : 나는 모든 4 개 개의 값을 서브 세트파이썬 - 내가 텍스트 파일에서 다음 입력 한 텍스트

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 열을 제거 할 숫자와 열의를 원하는 및 변경 이를 위해

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 (...) 

나는 다음과 같은 파이썬 코드 작성 제안 :

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() 
012,351,641을 두 번째의 3 값의 위치와 출력이 같아야합니다, 네 번째를 제거하므로

코드가 제대로 작동하는 것처럼 보일 수 있지만 문제는 각 줄마다 마지막 네 개의 값이 누락되어 있다는 것입니다. 그래서, 나는 그 문제가 "while"진술에 있다고 생각한다. 그러나 나는 그것을 해결하는 방법을 알지 못한다. (나는 그것이 간단한 문제로 보인다는 것을 안다.)

미리 제안 해 주셔서 감사합니다. (내가 후행 요소
4. 저장을 출력을 CSV 같은
1. 파일과 라벨을
2를 제거 원하는 크기
3의 하위 목록을 생성 스왑을 수행하고 제거 :

+2

그냥 참고 : 리소스를 사용하는 경우 수동으로 입력 스트림을 닫습니다 필요가 없습니다. 이것이 "함께"있는 이유입니다 : – oopbase

답변

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

이 시도 '

>>> 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 ^ 들여 쓰기 오류 : 들여 쓰기가 바깥 쪽 들여 쓰기 레벨과 일치하지 않습니다 – user2245731

+0

스크립트의 들여 쓰기가 잘되어있어서 실수로 복사 붙여 넣기를 할 수 있습니까? – xecgr

+0

무슨 뜻인지 잘 모르겠지만 이전 오류는 있지만 자세한 정보는 없습니다. 어쨌든, 내 코드에 오류가 어디에 있는지 실마리가 있습니까? 실수가 어디인지 이해하고 코딩 기술을 향상시킬 수 있습니다. – user2245731

관련 문제