1.数据集文件名称为英文:
import pandas as pd
app=pd.read_csv(r'C:\Users\25466\Desktop\test1.csv') #1 添加绝对路径地址
app.info() # 检测
解决方案:增加:encoding=‘utf-8’f = open(r'C:\Users\25466\Desktop\test1.csv',encoding='utf-8') #2
cct = pd.read_csv(f)
cct.head()
#报错: UnicodeDecodeError: 'gbk' codec can't decode byte 0x93 in position 1062: illegal multibyte sequence
#解决方案:encoding='utf-8'
3.解决方案:采用第二种方式
app=pd.read_csv(r'C:\Users\25466\Desktop\R项目数据.csv') #1
app.info()
# 报错:OSError: Initializing from file failed
# 解决方案:采用第二种方式
f = open(r'C:\Users\25466\Desktop\R项目数据.csv') #2
ppt = pd.read_csv(f)
ppt.head()
4.解决方案:增加,encoding=‘gbk’
kfc=pd.read_csv(r'C:\Users\25466\Desktop\Shared Bike Sample Data.csv',encoding='gbk') #1
kfc.info()
#报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 0: invalid start byte
#解决方案:增加,encoding='gbk'
#报错:SyntaxError: invalid character in identifier
# 检查字符,中英文符号等
规律总结:
报错:Initializing from file failed
原因:文件名为中文,直接报错
方案:果断使用方法2
f= open(r'绝对地址\文件名.csv') #2
任意名称 = pd.read_csv(f)
示例1:
示例2:
示例3:
如下:
f = open(r'C:\Users\25466\Desktop\销售目标设定 - Excel 示例数据.csv',encoding = 'utf-8') #2
fg = pd.read_csv(f)
fg.info()
#UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 22: illegal multibyte sequence
# ,encoding = 'utf-8'
只需切换方法二即可成功导入:
不断补充……