Stop solution Python file import error problem

Common

1. dataset file name in English:

import pandas as pd
app=pd.read_csv(r'C:\Users\25466\Desktop\test1.csv') #1 添加绝对路径地址
app.info() # 检测
  1. Solution: Add: encoding = 'utf-8'
    Here Insert Picture Description
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. Solution: The second wayHere Insert Picture Description

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. Solution: Add, encoding = 'gbk'
Here Insert Picture Description

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
# 检查字符,中英文符号等

Law Summary:

报错:Initializing from file failed

Cause: The file name is Chinese, direct error

Solution: Use decisively 2

f= open(r'绝对地址\文件名.csv') #2
任意名称 = pd.read_csv(f)

Example 1:
Here Insert Picture Description
Here Insert Picture Description
Example 2:
Here Insert Picture Description
Here Insert Picture Description
Example 3:
Here Insert Picture Description

Sometimes, on the basis of two methods may also be required to import the transcoded success! ! ! (VIP of a conclusion)

Namely: first use Method 1 to see if the import is not successful, if not successful, and error prompt: "Initializing from file failed", you can switch to the second method; not successful, usually transcoding problem, ko swap is successful .

as follows:

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'

Only two switching method can successfully import:

Here Insert Picture Description
Here Insert Picture Description

Two switching method can be successfully introduced, there remains a need for further transcoding

Here Insert Picture Description
Here Insert Picture Description
Continue to add ......

Published 17 original articles · won praise 10 · views 1679

Guess you like

Origin blog.csdn.net/weixin_44976611/article/details/104726302