🖥️ IT, 컴퓨터/🐍 Python

[Python] 파이썬 오류 해결 :: ParserError: Error tokenizing data. C error: EOF inside string starting at row

김 홍시 2024. 10. 12.
반응형

문제상황

30만 행 정도 되는 큰 csv를 파이썬에서 읽는 과정에서 오류가 발생했다. 

 

import pandas as pd
import csv

# CSV 파일 읽기 (에러 줄 건너뛰기)
file_path = 'fulldata_07_24_04_P_일반음식점.csv'
df = pd.read_csv(file_path, encoding='cp949')

이렇게 csv 파일 읽기 했는데 

---------------------------------------------------------------------------
ParserError                               Traceback (most recent call last)
<ipython-input-32-1aa32f4fb8a1> in <cell line: 6>()
      4 # CSV 파일 읽기 (에러 줄 건너뛰기)
      5 file_path = 'fulldata_07_24_04_P_일반음식점.csv'
----> 6 df = pd.read_csv(file_path, encoding='cp949')
      7 

3 frames
/usr/local/lib/python3.10/dist-packages/pandas/io/parsers/c_parser_wrapper.py in read(self, nrows)
    232         try:
    233             if self.low_memory:
--> 234                 chunks = self._reader.read_low_memory(nrows)
    235                 # destructive to chunks
    236                 data = _concatenate_chunks(chunks)

parsers.pyx in pandas._libs.parsers.TextReader.read_low_memory()

parsers.pyx in pandas._libs.parsers.TextReader._read_rows()

parsers.pyx in pandas._libs.parsers.TextReader._tokenize_rows()

parsers.pyx in pandas._libs.parsers.TextReader._check_tokenize_status()

parsers.pyx in pandas._libs.parsers.raise_parser_error()

ParserError: Error tokenizing data. C error: EOF inside string starting at row 48584

ParserError: Error tokenizing data. C error: EOF inside string starting at row 48584 

라는 오류 나옴 

 

 

 

 

 

해결 방법

csv 읽기에 오류가 있으니 txt로 변환해 읽어주면 어떨까 싶어서 했는데 성공함

 

단순히 파일명을 csv -> txt로 바꿔준 후 

import pandas as pd
import csv

# CSV 파일 읽기 (에러 줄 건너뛰기)
file_path = 'fulldata_07_24_04_P_일반음식점.txt'
df = pd.read_table(file_path, encoding='cp949')

경로를 csv가 아닌 txt로,

read_csv를 read_table로 바꿔주니 

잘 읽어짐 

반응형

댓글