您好,登錄后才能下訂單哦!
這篇文章主要介紹了怎么解決pandas read_csv讀取中文列標題文件報錯的問題,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
從windows操作系統本地讀取csv文件報錯
data = pd.read_csv(path)
Traceback (most recent call last): File "C:/Users/arron/PycharmProjects/ML/ML/test.py", line 45, in <module> data = pd.read_csv(path) File "C:\Users\arron\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 655, in parser_f return _read(filepath_or_buffer, kwds) File "C:\Users\arron\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 405, in _read parser = TextFileReader(filepath_or_buffer, **kwds) File "C:\Users\arron\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 762, in __init__ self._make_engine(self.engine) File "C:\Users\arron\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 966, in _make_engine self._engine = CParserWrapper(self.f, **self.options) File "C:\Users\arron\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1582, in __init__ self._reader = parsers.TextReader(src, **kwds) File "pandas\_libs\parsers.pyx", line 394, in pandas._libs.parsers.TextReader.__cinit__ (pandas\_libs\parsers.c:4209) File "pandas\_libs\parsers.pyx", line 712, in pandas._libs.parsers.TextReader._setup_parser_source (pandas\_libs\parsers.c:8895) OSError: Initializing from file failed
上述報錯的解決方案是
pwd = os.getcwd() os.chdir(os.path.dirname(path)) data = pd.read_csv(os.path.basename(path)) os.chdir(pwd)
此時又報如下錯誤:
Traceback (most recent call last): File "C:/Users/arron/PycharmProjects/ML/ML/test.py", line 47, in <module> data = pd.read_csv(os.path.basename(path)) File "C:\Users\arron\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 655, in parser_f return _read(filepath_or_buffer, kwds) File "C:\Users\arron\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 405, in _read parser = TextFileReader(filepath_or_buffer, **kwds) File "C:\Users\arron\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 762, in __init__ self._make_engine(self.engine) File "C:\Users\arron\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 966, in _make_engine self._engine = CParserWrapper(self.f, **self.options) File "C:\Users\arron\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1582, in __init__ self._reader = parsers.TextReader(src, **kwds) File "pandas\_libs\parsers.pyx", line 562, in pandas._libs.parsers.TextReader.__cinit__ (pandas\_libs\parsers.c:6175) File "pandas\_libs\parsers.pyx", line 781, in pandas._libs.parsers.TextReader._get_header (pandas\_libs\parsers.c:9691) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd7 in position 0: invalid continuation byte
最終解決方案是:
pwd = os.getcwd() os.chdir(os.path.dirname(path)) data = pd.read_csv(os.path.basename(path),encoding='gbk') os.chdir(pwd)
感謝你能夠認真閱讀完這篇文章,希望小編分享的“怎么解決pandas read_csv讀取中文列標題文件報錯的問題”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。