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

后端 未结 2 645
不知归路
不知归路 2021-01-01 09:26

I have been working on a program to retrieve questions from stack overflow. Till yesterday the program was working fine, but since today I\'m getting the error



        
相关标签:
2条回答
  • 2021-01-01 09:47

    I ran into this as well using Transifex API

    response['source_string']

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

    Fixed with response['source_string'].encode("utf-8")

    import requests
    
    username = "api"
    password = "PASSWORD"
    
    AUTH = (username, password)
    
    url = 'https://www.transifex.com/api/2/project/project-site/resource/name-of-resource/translation/en/strings/?details'
    
    response = requests.get(url, auth=AUTH).json()
    
    print response['key'], response['context']
    print response['source_string'].encode("utf-8")
    
    0 讨论(0)
  • 2021-01-01 09:53

    q.title is a Unicode string. When writing that to a file, you need to encode it first, preferably a fully Unicode-capable encoding such as UTF-8 (if you don't, Python will default to using the ASCII codec which doesn't support any character codepoint above 127).

    question.write(q.title.encode("utf-8"))
    

    should fix the problem.

    By the way, the program tripped up on character (U+201C).

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