繁体   English   中英

带有HTML特殊字符的熊猫read_csv()

[英]Pandas read_csv() with HTML special characters

我正在清理用逗号分隔的Python / Pandas中的CSV文件。

有些单元格具有& 作为文本的一部分。 当我运行read_csv()时,它会将分号视为当前单元格的末尾,并使行的其余部分偏移。

我试过encoding='utf8'和其他各种选项...

编辑**我的代码:

file = pd.read_csv('my-data-1.csv', encoding = 'utf8',index_col=False, low_memory=False)

file.drop(file.tail(1).index,inplace=True) #removing copyright line at the end


file_drop_dupes = file.drop_duplicates(['Project Id']) #drop the duplicates based on column Project Id

#drop all columns except these few
keep_col = ['Project Id','Project Name', 'Type']
new_file = file_drop_dupes[keep_col]
#write the result to a new csv file
new_file.to_csv('all-good-1.csv', index=False)

HTML字段的示例:

Service Maintenance & Supply

在python 3.4及更高版本中,它是一个简单的html.unescape() 在此之前,html.parser的HTMLParser.unescape() 看到这个答案

如果您使用的是python 3+ html.unescape()是解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM