Python 忽略Unicode错误
当我在一堆URL上运行一个循环以查找这些页面上的所有链接(在某些div中)时,我返回了以下错误:Python 忽略Unicode错误,python,unicode,csv,ascii,Python,Unicode,Csv,Ascii,当我在一堆URL上运行一个循环以查找这些页面上的所有链接(在某些div中)时,我返回了以下错误: Traceback (most recent call last): File "file_location", line 38, in <module> out.writerow(tag['href']) UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 0: ordinal n
Traceback (most recent call last):
File "file_location", line 38, in <module>
out.writerow(tag['href'])
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 0: ordinal not in range(128)
有没有办法解决这个问题,可以使用if语句忽略任何有Unicode错误的URL?
提前感谢您的帮助。您可以将writerow方法调用包装在
try
中,并捕获异常以忽略它:
for tag in soup_3.findAll('a', href=True):
try:
out.writerow(tag['href'])
except UnicodeEncodeError:
pass
但是你几乎肯定想为你的CSV文件选择一种非ASCII编码(utf-8,除非你有很好的理由使用其他编码),然后用
codecs.open()
而不是内置的open
打开它。非常感谢我使用了这个尝试:它很有效。如何更改编码,为什么要这样做?请原谅这个基本问题,但我对编程非常陌生。几乎总是,您不想因为数据恰好使用非ASCII字符而丢弃数据。如果你用open(“file_location”,“ab”,“utf-8”)
打开文件,而不是抛出UnicodeEncodeError
,输出。write
将写入它从网站读取的实际数据,99%的时间是你真正想要的。啊,当我添加“utf-8”时,这会有所帮助在当前打开的行的末尾,我得到了一个错误:TypeError:如果使用just open(“file_location”、“ab”、“utf-8”),则需要一个整数,如果是,我如何引入csv.writer,以便在“try:”部分使用它。再次感谢您的帮助;您需要编解码器。打开
(首先导入编解码器
),而不仅仅是打开
,正如我在上面的回答中所说,而不是在示例注释中。
for tag in soup_3.findAll('a', href=True):
try:
out.writerow(tag['href'])
except UnicodeEncodeError:
pass