반응형
Pandas로 탭으로 구분 된 파일 읽기 -Windows에서는 작동하지만 Mac에서는 작동하지 않습니다
Windows에서 Pandas / Python으로 탭으로 구분 된 데이터 파일을 문제없이 읽고 있습니다. 데이터 파일은 처음으로 줄에 메모를 포함하고 그 세 줄에 있습니다.
df = pd.read_csv(myfile,sep='\t',skiprows=(0,1,2),header=(0))
이제 Mac에서 파일을 발견합니다. (Mac에서 Python을 처음 사용했습니다.) 다음과 같은 오류가 발생합니다.
pandas.parser.CParserError: Error tokenizing data. C error: Expected 1
fields in line 8, saw 39
read_csv 에 대한 error_bad_lines 인수 를 False로 설정하면 다음 정보가 표시되고 마지막 행이 끝날 때까지 계속됩니다.
Skipping line 8: expected 1 fields, saw 39
Skipping line 9: expected 1 fields, saw 125
Skipping line 10: expected 1 fields, saw 125
Skipping line 11: expected 1 fields, saw 125
Skipping line 12: expected 1 fields, saw 125
Skipping line 13: expected 1 fields, saw 125
Skipping line 14: expected 1 fields, saw 125
Skipping line 15: expected 1 fields, saw 125
Skipping line 16: expected 1 fields, saw 125
Skipping line 17: expected 1 fields, saw 125
...
인코딩 인수에 대한 값을 지정해야 합니까? 파일 읽기가 Windows에서 잘 작동하기 때문에 필요하지 않은 것처럼 보입니다.
가장 큰 단서는 행이 모두 한 줄에 돌아옵니다. 이것은 줄 종결 롭거나 존재하지 않습니다.
csv_reader에 대한 줄 종결 할 수 있습니다. 당신이로 끝납니다 만든 선 맥에있는 경우 \r
가 \n
아닌 리눅스 표준 더 나은 여전히 나에 윈도우의 멜빵과 벨트 방식은 \r\n
.
pandas.read_csv(filename, sep='\t', lineterminator='\r')
코덱 패키지를 사용하여 모든 데이터를 열 수도 있습니다. 이 문서 로딩 속도를 느리게 선언합니다.
import codecs
doc = codecs.open('document','rU','UTF-16') #open for reading with "universal" type set
df = pandas.read_csv(doc, sep='\t')
또 다른 옵션은 engine='python'
명령 에 추가 하는 것입니다.pandas.read_csv(filename, sep='\t', engine='python')
반응형
'ProgramingTip' 카테고리의 다른 글
RabbitMQ 대신 Celery를 사용하는 이유는 무엇입니까? (0) | 2020.11.28 |
---|---|
IIS URL 재 작성 {R : N} 설명 (0) | 2020.11.28 |
py.test 테스트 내에서 로깅 (0) | 2020.11.28 |
추가해야하는 이유 (0) | 2020.11.28 |
Eclipse에서 사용할 때 matplotlib의 기존 그림 닫기 (0) | 2020.11.28 |