在Python py中导入csv文件

kognpnkq  于 12个月前  发布在  Python
关注(0)|答案(2)|浏览(94)

我想在google colab中导入我的csv文件。

import pandas as pd

a = “/content/drive/MyDrive/ordner/Mappe 2 (1).xlsx”
b = pd.read_csv(a, engine=“python”, encoding=“utf-8”)
b.head()

但它给了我这个错误:
Unicode解码错误:'utf-8'编解码器无法解码位置10中的字节0xa 6:无效起始字节
我尝试使用engine=、encoding=或“//”,但它总是不起作用。我不知道,是什么问题还是我写错了我的Excel文件?
enter image description here
有人能帮帮我吗

1u4esq0p

1u4esq0p1#

您正在使用read_csv,但您有一个xlsx文件。请使用以下选项:

import pandas as pd

# Specify the path to the Excel file
excel_file = '/content/drive/MyDrive/ordner/Mappe 2 (1).xlsx'

# Read the Excel file into a pandas DataFrame
df = pd.read_excel(excel_file)

# Display the DataFrame
print(df)

df.head()
kpbpu008

kpbpu0082#

尝试使用read_excel与engine:
variable_name = pd.read_excel(r'filename.xlsx', engine='openpyxl')
您可以尝试另一种方法,例如使用电子表格将“filename.xlsx”转换为“filename.csv”。我希望这能帮上忙。

相关问题