Python : UnicodeEncodeError: 'latin-1' codec can't encode character

后端 未结 3 1299
别跟我提以往
别跟我提以往 2021-01-04 05:34

I am at a scenario where I call api and based on the results from api I call database for each record that I in api. My api call return strings and when I make the database

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 05:57

    If you need Latin-1 encoding, you have several options to get rid of the en-dash or other code points above 255 (characters not included in Latin-1):

    >>> u = u'hello\u2013world'
    >>> u.encode('latin-1', 'replace')    # replace it with a question mark
    'hello?world'
    >>> u.encode('latin-1', 'ignore')     # ignore it
    'helloworld'
    

    Or do your own custom replacements:

    >>> u.replace(u'\u2013', '-').encode('latin-1')
    'hello-world'
    

    If you aren't required to output Latin-1, then UTF-8 is a common and preferred choice. It is recommended by the W3C and nicely encodes all Unicode code points:

    >>> u.encode('utf-8')
    'hello\xe2\x80\x93world'
    

提交回复
热议问题