Python-Unicode和双反斜杠 [英] Python - Unicode & double backslashes
问题描述
我用BeautifulSoup抓取了一个网页.在得到文本之后,除了列表的部分如下所示,我得到了很好的输出:
I scrapped a webpage with BeautifulSoup. I got great output except parts of the list look like this after getting the text:
list = [u'that\\u2019s', u'it\\u2019ll', u'It\\u2019s', u'don\\u2019t', u'That\\u2019s', u'we\\u2019re', u'\\u2013']
我现在的问题是如何摆脱这些双反斜杠或将其替换为特殊字符.
My question now is how to get rid or replace these double backslashes with the special characters they are.
如果我先打印示例列表的第一元素,则输出看起来像
If i print the first the first element of the example list the output looks like
print list[0]
that\u2019s
我已经阅读了许多有关此主题的其他问题/线索,但最终变得更加困惑,因为我是正在考虑unicode/编码/解码的初学者.
I already read a lot of other questions / threads about this topic but I ended up being even more confused, as I am a beginner considering unicode / encoding / decoding.
我希望有人可以帮助我解决这个问题.
I hope that someone could help me with this issue.
谢谢!MG
推荐答案
这里的问题是该站点最终对那些unicode参数进行了双重编码,只需执行以下操作:
the problem here is that the site ended up double encoding those unicode arguments, just do the following:
ls = [u'that\\u2019s', u'it\\u2019ll', u'It\\u2019s', u'don\\u2019t', u'That\\u2019s', u'we\\u2019re', u'\\u2013']
ls = map(lambda x: x.decode('unicode-escape'), ls)
现在您有了一个包含正确的unicode编码字符串的列表:
now you have a list with properly unicode encoded strings:
for a in ls:
print a
这篇关于Python-Unicode和双反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!