经常遇到小伙伴,文件读取乱码问题,今天把所有方法都给你
1、读csv
filename='xxx.csv' pd.read_csv(filename) pd.read_csv(filename,encoding='GBK') pd.read_csv(filename,encoding='utf-8') pd.read_csv(filename,encoding='utf-8-sig') pd.read_csv(filename,engine='python',encoding='GBK') pd.read_csv(filename,engine='python',encoding='utf-8') pd.read_csv(filename,engine='python',encoding='utf-8-sig')
2、读xlsx、xls
filename='xxx.xlsx' 或者 filename='xxx.xls' pd.read_excel(filename) pd.read_excel(filename,encoding='GBK') pd.read_excel(filename,encoding='utf-8')
3、写csv
filename='xxx.csv' df.to_csv(filename,encoding='utf-8-sig')
4、写xlsx
filename='xxx.xlsx' df.to_excel(filename,index=None)
以上方法,总有一款适合你。