从wiki读取csv文件时发生CParserError

2024-06-26 10:25:03 发布

您现在位置:Python中文网/ 问答频道 /正文

the Wikipedia pageon college towns是美国大学城的一个列表,它被复制并粘贴到university_towns.txt文件中。你知道吗

我使用pandas.read_csv('university_towns.txt')读取文件,但得到CParserError消息。你知道吗

我也尝试设置error_bad_lines=False,但它会导致一些数据丢失。你知道吗

CParserError: Error tokenizing data. C error: Expected 1 fields in line 8,saw 3

Tags: 文件thetxtpandas列表read粘贴error
1条回答
网友
1楼 · 发布于 2024-06-26 10:25:03

不清楚粘贴到university_towns.txt后对列表引入了哪些更改。不过,快速查看一下列表可以发现,许多大学的名称都包含逗号,。这就是导致pandas.read_csv失败的原因

Leeds (Leeds College of Art, Leeds College of Music, Northern School of Contemporary Dance, Leeds Metropolitan University, Leeds Trinity University, University of Leeds)

这是因为,正如在pandas.read_csvdocumentation中所解释的,逗号被用作默认分隔符","。快速解决方法是按以下步骤更换分离器:

pandas.read_csv('university_towns.txt', sep="|")

相关问题 更多 >