Removing \u2018 and \u2019 character

前端 未结 1 1675
悲&欢浪女
悲&欢浪女 2020-12-09 15:22

I am using Beautiful Soup to parse webpages and printing the name of the webpages visited on the terminal. However, often the name of the webpage has single left (\\u2018

相关标签:
1条回答
  • 2020-12-09 15:43

    These codes are Unicode for the single left and right quote characters. You can replace them with their ASCII equivalent which Python shouldn't have any problem printing on your system:

    >>> print u"\u2018Hi\u2019"
    ‘Hi’
    >>> print u"\u2018Hi\u2019".replace(u"\u2018", "'").replace(u"\u2019", "'")
    'Hi'
    

    Alternatively with regex:

    import re
    s = u"\u2018Hi\u2019"
    >>> print re.sub(u"(\u2018|\u2019)", "'", s)
    'Hi'
    

    However Python shouldn't have any problem printing the Unicode version of these as well. It's possible that you are using str() somewhere which will try to convert your unicode to ascii and throw your exception.

    0 讨论(0)
提交回复
热议问题