繁体   English   中英

将文件写入csv时出现解码问题

[英]decoding issue when writing a file to csv

使用以下代码,我尝试从具有特殊字符的url中提取描述。

from bs4 import BeautifulSoup
import urllib.request
import pandas as pd
html = urllib.request.urlopen('http://uk.rs-online.com/web/p/piezoelectric-
miniature-speakers/7868948/').read()
soup = BeautifulSoup(html)
description = soup.find(itemprop="name").string.strip()
description
pd.DataFrame([description]).to_csv('file.csv')

查看csv文件中的抓取数据后,我发现那些特殊字符已替换为问号。

如何在csv文件中获取那些特殊字符。

预先感谢您的建议。

选择正确的编码,特殊字符将出现在文件中。 我使用utf8进行了测试,并且所有特殊字符均正确显示。

from bs4 import BeautifulSoup
import urllib.request
import pandas as pd
html = urllib.request.urlopen('http://uk.rs-online.com/web/p/piezoelectric-miniature-speakers/7868948/').read()
soup = BeautifulSoup(html)
description = soup.find(itemprop="name").string.strip()
pd.DataFrame([description]).to_csv('file.csv', encoding='utf8')

还要确保在编辑器中以正确的编码打开文件

暂无
暂无

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

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