读取文件的熊猫

问题描述 投票:0回答:1

我正在尝试使用 pandas 读取存储在我的计算机中的 csv 文件。 这是我的代码,但我有一个像屏幕截图一样的问题。

file_path = "/Users/shazelpark/laptop_pricing_dataset_mod1.csv"
df = pd.read_csv(file_path, header=0)

我想毫无问题地阅读该文件。 请分享您的解决方案。

谢谢

python pandas read.csv
1个回答
0
投票

尝试这样

import pandas as pd

file_path = "/Users/shazelpark/laptop_pricing_dataset_mod1.csv"

try:
    df = pd.read_csv(file_path, header=0)
    print(df.head())  
except FileNotFoundError:
    print("File not found. Please check the file path.")
except pd.errors.EmptyDataError:
    print("The file appears to be empty.")
except pd.errors.ParserError:
    print("There was an issue parsing the file. Please check the file format.")
© www.soinside.com 2019 - 2024. All rights reserved.