帮助替换 Python 中的非 ASCII 字符 [英] Help Replacing Non-ASCII character in Python

查看:64
本文介绍了帮助替换 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&nbsp;</font> is the desired format.

如何在 Python 中用 '&nbsp;' 替换 'Â '?非常感谢!

How do I replace the 'Â ' with '&nbsp;' 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.

这篇关于帮助替换 Python 中的非 ASCII 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆