pandas 继续获取ParseError

yyhrrdl8  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(88)

我试图读取这个txt文件。但由于某些原因,我一直得到这个错误:
解析器错误:标记数据时出错。C错误:第27行中预期有1个字段,结果为367
下面是我的代码:

df = pd.read_csv('githublink')  
df.read()

Data from the githublink:

cgh8pdjw

cgh8pdjw1#

您必须使用GitHub中的Raw链接,因为您提供的链接返回HTML页面。

import pandas as pd
import io
import requests
url="https://raw.githubusercontent.com/ryan-2121/data/595128c71d558943d0fbc518dcca618a7273d058/product_care"  # using the raw link from github
s=requests.get(url).content
df = pd.read_csv(io.StringIO(s.decode()), sep=",", header=None, names = ['Region','Market','Chart',"Name","Unit","Source","2014","2015","2016","2017",'2018',"2019","2020","2021","2022",'2023',"2024","2025","2026","2027"] )
df.drop(index=df.index[0], axis=0, inplace=True) # The first row contains the column name, hence dropping it.

相关问题