UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)

后端 未结 29 2817
余生分开走
余生分开走 2020-11-21 04:43

I\'m having problems dealing with unicode characters from text fetched from different web pages (on different sites). I am using BeautifulSoup.

The problem is that

29条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 05:05

    I found elegant work around for me to remove symbols and continue to keep string as string in follows:

    yourstring = yourstring.encode('ascii', 'ignore').decode('ascii')
    

    It's important to notice that using the ignore option is dangerous because it silently drops any unicode(and internationalization) support from the code that uses it, as seen here (convert unicode):

    >>> u'City: Malmö'.encode('ascii', 'ignore').decode('ascii')
    'City: Malm'
    

提交回复
热议问题