解決Python中pandas讀取*.csv文件出現(xiàn)編碼問題
1、問題
在使用Python中pandas讀取csv文件時(shí),由于文件編碼格式出現(xiàn)以下問題:
Traceback (most recent call last): File "pandas\_libs\parsers.pyx", line 1134, in pandas._libs.parsers.TextReader._convert_tokens File "pandas\_libs\parsers.pyx", line 1240, in pandas._libs.parsers.TextReader._convert_with_dtype File "pandas\_libs\parsers.pyx", line 1256, in pandas._libs.parsers.TextReader._string_convert File "pandas\_libs\parsers.pyx", line 1494, in pandas._libs.parsers._string_box_utf8 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 19: invalid start byte During handling of the above exception, another exception occurred: Traceback (most recent call last): File "E:\PyCharm 2017.3.4\helpers\pydev\pydevd.py", line 1668, in <module> main() File "E:\PyCharm 2017.3.4\helpers\pydev\pydevd.py", line 1662, in main globals = debugger.run(setup['file'], None, None, is_module) File "E:\PyCharm 2017.3.4\helpers\pydev\pydevd.py", line 1072, in run pydev_imports.execfile(file, globals, locals) # execute the script File "E:\PyCharm 2017.3.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "F:/OneDrive - emails.bjut.edu.cn/Program/Python/DCAE/test.py", line 18, in <module> load_phenotypes_ABIDE2_RfMRIMaps() File "F:/OneDrive - emails.bjut.edu.cn/Program/Python/DCAE\Data\load_data.py", line 109, in load_phenotypes_ABIDE2_RfMRIMaps pheno = pd.read_csv(pheno_path) File "E:\Python\Python35\lib\site-packages\pandas\io\parsers.py", line 678, in parser_f return _read(filepath_or_buffer, kwds) File "E:\Python\Python35\lib\site-packages\pandas\io\parsers.py", line 446, in _read data = parser.read(nrows) File "E:\Python\Python35\lib\site-packages\pandas\io\parsers.py", line 1036, in read ret = self._engine.read(nrows) File "E:\Python\Python35\lib\site-packages\pandas\io\parsers.py", line 1848, in read data = self._reader.read(nrows) File "pandas\_libs\parsers.pyx", line 876, in pandas._libs.parsers.TextReader.read File "pandas\_libs\parsers.pyx", line 891, in pandas._libs.parsers.TextReader._read_low_memory File "pandas\_libs\parsers.pyx", line 968, in pandas._libs.parsers.TextReader._read_rows File "pandas\_libs\parsers.pyx", line 1094, in pandas._libs.parsers.TextReader._convert_column_data File "pandas\_libs\parsers.pyx", line 1141, in pandas._libs.parsers.TextReader._convert_tokens File "pandas\_libs\parsers.pyx", line 1240, in pandas._libs.parsers.TextReader._convert_with_dtype File "pandas\_libs\parsers.pyx", line 1256, in pandas._libs.parsers.TextReader._string_convert File "pandas\_libs\parsers.pyx", line 1494, in pandas._libs.parsers._string_box_utf8 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 19: invalid start byte
我認(rèn)為該問題是由于文件編碼格式不是'utf-8'所導(dǎo)致的,因此,嘗試將文件格式進(jìn)行轉(zhuǎn)換,轉(zhuǎn)換方式如下:
首先使用txt文本打開文件,然后另存為,在右下角將編碼改為‘UTF-8',點(diǎn)擊保存即可

總結(jié)
以上所述是小編給大家介紹的解決Python中pandas讀取*.csv文件出現(xiàn)編碼問題 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
PyTorch之怎樣選擇合適的優(yōu)化器和損失函數(shù)
這篇文章主要介紹了PyTorch怎樣選擇合適的優(yōu)化器和損失函數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Python3.10動(dòng)態(tài)修改Windows系統(tǒng)本地IP地址
這篇文章主要介紹了Python3.10動(dòng)態(tài)修改Windows系統(tǒng)本地IP地址,需要的朋友可以參考下2023-05-05
python web應(yīng)用程序之Django數(shù)據(jù)庫詳解
這篇文章主要介紹了python web應(yīng)用程序之Django數(shù)據(jù)庫,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
Python自動(dòng)爬取圖片并保存實(shí)例代碼
大家好,本篇文章主要講的是Python自動(dòng)爬取圖片并保存實(shí)例代碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
python把數(shù)組中的數(shù)字每行打印3個(gè)并保存在文檔中的方法
今天小編就為大家分享一篇python把數(shù)組中的數(shù)字每行打印3個(gè)并保存在文檔中的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
python虛擬環(huán)境venv、virtualenv配置方式
這篇文章主要介紹了python虛擬環(huán)境venv、virtualenv配置方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Python中使用sqlalchemy操作數(shù)據(jù)庫的問題總結(jié)
在探索使用?FastAPI,?SQLAlchemy,?Pydantic,Redis,?JWT?構(gòu)建的項(xiàng)目的時(shí)候,其中數(shù)據(jù)庫訪問采用SQLAlchemy,并采用異步方式,這篇文章主要介紹了在Python中使用sqlalchemy來操作數(shù)據(jù)庫的幾個(gè)小總結(jié),需要的朋友可以參考下2024-08-08
python安裝并使用virtualenv管理包的詳細(xì)過程
本文主要介紹了Python的安裝過程和如何使用virtualenv管理包,首先,用戶需要訪問Python官網(wǎng)下載安裝包,并運(yùn)行安裝程序,安裝完成后,在命令行輸入Python,顯示安裝的Python版本號,即表示安裝成功,感興趣的朋友一起看看吧2024-10-10

