def read_csv(filenames, root='.',encoding1='utf-8',encoding2='utf-8',header=None,names=None):
"""
直接采用pd.read_csv难以处理文件名中带有中文的文件,需要借助open函数。
直接采用with open('xx.csv') as f 的方式难以处理csv的中文列名。
故最终采用with + codecs.open,能够同时解决上述两个问题。
注意:如果源文件是ansi格式,则应将encoding1设置为'gbk'.
"""
with codecs.open(root+'/'+filenames,'r',encoding1) as f:
if names != None:
df = pd.read_csv(f,encoding=encoding2,names=names,low_memory=False)
else:
df = pd.read_csv(f,encoding=encoding2,header=header,low_memory=False)
return df
解决pandas.read_csv读取文件名带有中文或者内容带有中文的办法
最新推荐文章于 2025-03-11 15:29:01 发布