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

后端 未结 29 2811
余生分开走
余生分开走 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

    You need to read the Python Unicode HOWTO. This error is the very first example.

    Basically, stop using str to convert from unicode to encoded text / bytes.

    Instead, properly use .encode() to encode the string:

    p.agent_info = u' '.join((agent_contact, agent_telno)).encode('utf-8').strip()
    

    or work entirely in unicode.

提交回复
热议问题