Python - Unicode & double backslashes

前端 未结 2 846
野性不改
野性不改 2021-01-24 03:44

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\\         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-24 04:19

    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)
    

    now you have a list with properly unicode encoded strings:

    for a in ls:
       print a
    

提交回复
热议问题