为何pandas.read_csv不能读取中文内容而使用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")

这是为什么呢?

阅读 5.4k
1 个回答

因为read_csv的第一个参数是:

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函数是支持中文名字的,所以不会出现打开错误

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题