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

后端 未结 29 2818
余生分开走
余生分开走 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 04:51

    A subtle problem causing even print to fail is having your environment variables set wrong, eg. here LC_ALL set to "C". In Debian they discourage setting it: Debian wiki on Locale

    $ echo $LANG
    en_US.utf8
    $ echo $LC_ALL 
    C
    $ python -c "print (u'voil\u00e0')"
    Traceback (most recent call last):
      File "", line 1, in 
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 4: ordinal not in range(128)
    $ export LC_ALL='en_US.utf8'
    $ python -c "print (u'voil\u00e0')"
    voilà
    $ unset LC_ALL
    $ python -c "print (u'voil\u00e0')"
    voilà
    

提交回复
热议问题