저는 Python 2.7을 사용하고 있습니다. 나는 Python을 처음 접했을 뿐이다. CSV 파일 (값은 공백으로 구분됨)을 읽고 좌표 위의 머리글을 기준으로 값을 분리하려고합니다. 파일 형식은 필자가 익숙한 것이 아니며 값을 올바르게 읽는 데 문제가 있습니다. 비록 내가 올바르게 읽을 수 있다고해도, 나는 그것을 어떻게 목록에 넣는 지 이해하지 못합니다.Python 2를 사용하여 CSV 파일 읽기
class CommentedFile:
def __init__(self, f, commentstring="#"):
self.f = f
self.commentstring = commentstring
def next(self):
line = self.f.next()
while line.startswith(self.commentstring):
line = self.f.next()
return line
def __iter__(self):
return self
#I did this in order to ignore the comments in the CSV file
tsv_file = csv.reader(CommentedFile(open("test.exp", "rb")),
delimiter=' ')
for row in tsv_file:
if row != int:
next(tsv_file)
if row:
print row
코드가 인쇄됩니다 :
['100', '100']
['100', '200']
['100', '200']
['300', '300']
Traceback (most recent call last):
File "the path", line 57, in <module>
next(tsv_file)
StopIteration
그래서 내가 '여기
# image name
1.png
# probe locations
100 100
200 100
100 200
300 300
# another image name
2.png
100 200
200 100
300 300
135 322
# end
내가 함께 연주하고있는 코드입니다 : 여기
은 CSV 파일의 모습입니다 프로그램이 헤더를 기반으로 좌표를 분리하고 별도의 목록에 넣으려고하는 중입니다. 도와 줘서 고마워!
'CSV를 .reader'. 예제 입력 파일에서 원하는 출력은 무엇입니까? – Aya
'if row! = int :'줄을 이해할 수 없습니까? ??? –
필자는 드로잉 할 그래픽의 좌표로 독자/파서의 출력을 사용하고 싶습니다. 그래서리스트는 x와 y 좌표가 함께 출력되는 방식을 함께 생각합니다. csv.reader 대신 무엇을 사용해야합니까? – user2479054