개발/Python

Windows pandas read_csv UnicodeDecodeError

강서버 2023. 11. 18. 17:57
728x90
반응형

[ Code ]

---------------------------------------------------------------------------

import pandas as pd

df1 = pd.read_csv('sample.csv')

---------------------------------------------------------------------------

 

[ Error message ]

C:\py_test> python sample.py


Traceback (most recent call last):
  File "C:\py_test\sample.py", line 3, in <module>
    df1 = pd.read_csv('sample.csv')
  File "C:\Python\Python39\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Python\Python39\lib\site-packages\pandas\io\parsers\readers.py", line 680, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Python\Python39\lib\site-packages\pandas\io\parsers\readers.py", line 575, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Python\Python39\lib\site-packages\pandas\io\parsers\readers.py", line 933, in __init__
    self._engine = self._make_engine(f, self.engine)
  File "C:\Python\Python39\lib\site-packages\pandas\io\parsers\readers.py", line 1235, in _make_engine
    return mapping[engine](f, **self.options)
  File "C:\Python\Python39\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 75, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas\_libs\parsers.pyx", line 544, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas\_libs\parsers.pyx", line 633, in pandas._libs.parsers.TextReader._get_header
  File "pandas\_libs\parsers.pyx", line 847, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas\_libs\parsers.pyx", line 1952, in pandas._libs.parsers.raise_parser_error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 0: invalid continuation byte

[ solve ]

pandas read_csv encoding add (encoding='cp949')

 

---------------------------------------------------------------------------

import pandas as pd

df1 = pd.read_csv('sample.csv', encoding='cp949')

----------------------------------------------------------------------------

 

728x90
반응형