帮助替换 Python 中的非 ASCII 字符
问题描述:
我有一堆使用 Python 中的 HTTPLIB2 包下载的 HTML 文件.' '显示为Â".
I have a bunch of HTML files I downloaded using HTTPLIB2 package in Python. ' ' are showing as 'Â '.
<font color="#ff0000">02/12/2004Â </font> is showing while <font color="#ff0000">02/12/2004 </font> is the desired format.
如何在 Python 中用 ' '
替换 'Â '
?非常感谢!
How do I replace the 'Â '
with ' '
in Python? Thanks a lot!
答
您遇到了编码问题.与其尝试删除这些字符,不如查找页面的编码,然后在读取文件时使用 codecs
模块 而不是 open()
,使用正确的字符编码.
You've got an encoding problem. Instead of trying to remove this characters, look for the encoding of the page, then when you read the file, use the codecs
module instead of open()
, using the proper character encoding.