【发布时间】:2014-07-21 19:25:46
【问题描述】:
我正在尝试在 python 中解码 u'\uf04a',因此我可以在没有错误警告的情况下打印它。换句话说,我需要将愚蠢的 microsoft Windows 1252 字符转换为实际的 unicode
包含异常错误的html来源来自这里http://members.lovingfromadistance.com/showthread.php?12338-HAVING-SECOND-THOUGHTS
点击此处http://www.fileformat.info/info/unicode/char/f04a/index.htm了解有关 u'\uf04a' 和 u'\uf04c' 的信息
一个例子如下所示:
"Oh god please some advice ":
Out[408]: u'Oh God please some advice \uf04c'
以这样的线程为例进行测试:
thread = u'who are you \uf04a Why you are so harsh to her \uf04c'
thread.decode('utf8')
print u'\uf04a'
print u'\uf04a'.decode('utf8') # error!!!
'charmap' 编解码器无法对位置 1526 中的字符 u'\uf04a' 进行编码:字符映射到未定义
在两个 Python 脚本的帮助下,我成功转换了 u'\x92',但我仍然卡在 u'\uf04a' 上。有什么建议吗?
参考文献
https://github.com/AnthonyBRoberts/NNS/blob/master/tools/killgremlins.py
Handling non-standard American English Characters and Symbols in a CSV, using Python
解决方案:
根据下面的cmets:我将这些字符集替换为问号('?')
thread = u'who are you \uf04a Why you are so harsh to her \uf04c'
thread = thread.replace(u'\uf04a', '?')
thread = thread.replace(u'\uf04c', '?')
希望这对其他初学者有所帮助。
【问题讨论】:
-
不清楚您要做什么,或者 Windows 1252 的来源。您真正要打印的字符是什么?你从哪里得到数据?如果将该“字符串”视为字节序列,则它不是有效的 UTF-8...
-
我同意。上面的帖子已经修改了。
标签: python unicode decode cp1252