UnicodeEncodeError:'ascii'编解码器无法对位置142中的字符'\ u030c'进行编码:序数不在范围内(128)

时间:2015-04-01 08:15:54

标签: python python-3.x unicode encoding

import json
import codecs
from urllib.request import urlopen

url = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=myapikeyhere&lang=hr-en&text="
word_list = codecs.open("inputfile.txt", encoding='utf-8').read().split(" ")
recnik = codecs.open('translation.txt', encoding='utf-8', mode='w')

for search_term in word_list:
    response = urlopen(url+search_term)
    translation = json.loads(response.readall().decode('utf-8'))
    translation = translation['text'][0]
    one_string = search_term + " " + translation + '\n'

    recnik.write(one_string)

我试图使用yandex.ru的API在文本文件中翻译我的几个单词。在翻译前几个单词后,我收到以下错误:

UnicodeEncodeError: 'ascii' codec can't encode character '\u030c' in position 142: ordinal not in range(128)

当它遇到我的wordlist中的第一个带有非ascii字符的单词时会发生这种情况。据我所知,我在unicode中做了一切,但我可​​能错过了一些明显的东西,我会很感激别人的眼睛。

此行发生错误:

response = urlopen(url+search_term) 

编辑:

我使用的解决方案,以防将来可以帮助某人:

import json
import codecs
from urllib.request import urlopen
from urllib.parse import quote 

url = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=myapikey=hr-en&text="
word_list = codecs.open("inputfile.txt", encoding='utf-8').read().split(" ")
recnik = codecs.open('translation.txt', encoding='utf-8', mode='w')

for search_term in word_list:
    send = url+quote(search_term)
    response = urlopen(send)
    translation = json.loads(response.readall().decode('utf-8'))
    translation = translation['text'][0]
    one_string = search_term + " " + translation
    print(one_string)
    one_string += '\n'
    recnik.write(one_string)

0 个答案:

没有答案