鍍金池/ 問答/數(shù)據(jù)分析&挖掘  Python  網(wǎng)絡安全/ 為何pandas.read_csv不能讀取中文內(nèi)容而使用f=open()之后就可

為何pandas.read_csv不能讀取中文內(nèi)容而使用f=open()之后就可以了?

直接用src_read=pd.read_csv(src_dir+"\"+src_file_nm) OSError: Initializing from file failed

clipboard.png
但是用open之后就可以了
f=open(src_dir+"\"+src_file_nm)
src_read=pd.read_csv(f,dtype="str")

這是為什么呢?

回答
編輯回答
祉小皓

因為read_csv的第一個參數(shù)是:

filepath_or_buffer : str, pathlib.Path, py._path.local.LocalPath or any object with a read() method (such as a file handle or StringIO)

所以可以接受open之后的io對象,而open函數(shù)是支持中文名字的,所以不會出現(xiàn)打開錯誤

2017年1月3日 22:34