我正在将goslate
用于google translate API
我能把孟加拉语翻译成英语-
>>> import goslate
>>> gs = goslate.Goslate()
>>> S = gs.translate("ভাল", 'en')
>>> S
good
但是,当我想把英语翻译成孟加拉语时,问题就出现了。在
^{pr2}$E或:
return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-2: character maps to <undefined>
我该怎么办?在
print repr(S)
output: u'\u09ad\u09be\u09b2'
print("ভাল")
output: à¦à¦¾à¦²
print(u"ভাল") # this gives UnicodeEncodeError
它绝对与
goslate
无关。您的问题是在无法使用控制台字符编码表示Unicode字符时使print u'\u09ad\u09be\u09b2'
工作。在您要么需要将编码更改为可以表示Unicode字符的编码,如utf-8,要么使用Unicode API,如
WriteConsoleW
。假设您在Windows上,如果您不在Windows上,那么只需将您的环境配置为使用utf-8。在^{} usage is complicated 虽然有一个simple to use ^{} package on Python 3 。后一个链接还显示了如何将打印的Unicode文本保存到文件(print Unicode,set
PYTHONIOENCODING
)。在这对我有用
这将按预期打印“ভাল”。
print st.encode('utf-8')
也可以。在相关问题 更多 >
编程相关推荐