2017-03-17 3 views
0

txt 파일을 읽는 동안 몇 가지 문제가 있습니다. 내가해야 할 일은 파일 (약 360)을 읽고 음모를 꾸미는 것입니다. 내 파일에 특수 문자가있는 경우를 제외하고는 모두 ":". 내 독서 기능이 문자를 찾으면 충돌이 발생합니다. 그것을 건너 뛸 방법이 있습니까? 내 코드 : 그 "$"줄을 건너 뛸 수있는 방법이 있습니까 enter image description here : 여기txt 파일의 핫 스킵 특수 문자 python

import os 
import matplotlib.pyplot as plt 
import numpy as np 

i = 10 
j = 0 
X = [] 
Y = [] 
Z = [] 
k = 0 
A = np.zeros([360,719]) 

for i in range(10,360,10): 
     X = [] 
     Y = [] 
     if len(str(i)) == 2: 
      data = open(dir + '\\150317_ScPONd_0%s_radio.txt'%i, 'r') 
     else: 
      data = open(dir + '\\150317_ScPONd_%s_radio.txt'%i, 'r') 
     z = data.readlines() 
     data.close() 
     for line in z: 
      if not line.startswith('$'): 
       data_2 = line.split('\t') 
       X.append(data_2[0]) 
       Y.append(data_2[1]) 
     A[j,:] = X 
     A[(j+1),:] = Y 

그리고 내 파일의 모습을 어떻게? 그 그림을 유감스럽게 생각합니다. 어떻게 더 잘 붙일 수 있을지 모르겠습니다.

+0

왜 np.genfromtxt를 사용하지 않습니까? – plasmon360

+0

if line.startswith ('$') == False :'? – WhatsThePoint

+0

@WhatsThePoint 예 시도했지만 실패했습니다. – Maq92

답변

0

탁신 to @ user1753919 답변을 찾았습니다. 누군가가 여전히이에 관심이 될 경우, 여기에 코드를 작동 :

for i in range(10,360,10): 
     X = [] 
     Y = [] 
     if len(str(i)) == 2: 
      data = np.genfromtxt(dir + '\\150317_ScPONd_0%s_radio.txt'%i,skip_header = 12) 
     else: 
      data = np.genfromtxt(dir + '\\150317_ScPONd_%s_radio.txt'%i,skip_header = 12) 
     for line in data: 
      X.append(line[0]) 
      Y.append(line[1]) 
     A[j,:] = X 
     A[(j+1),:] = Y 
     plt.plot(A[j,:],A[(j+1),:],label = '{} K'.format(i)) 
     plt.hold 
     j = j+2 
0

genfromtxt은 과잉이다.

np.loadtxt(file, comments='$')