土耳其字符编码 [英] Turkish character encoding
问题描述
我尝试从不同的列表项创建新的句子。当我通过unicode打印它的错误。
我可以正常打印(没有unicode)。当我尝试将其发布到网站时,它发生同样的错误。我想,如果我可以用unicode解决它,它将工作,当ı发布到网站。
I try to create new sentence from different list items. Its giving error when I print it by unicode. I can print it normally (without unicode). When I try to post it to the web site its rasing same error. I tought that if I can fix it with unicode, it will work when ı post it to the website.
p=['Bu', 'Şu']
k=['yazı','makale']
t=['hoş','ilgiç']
connect='%s %s %s'%(p[randint(0,len(p)-1)],k[randint(0,len(k)-1)],t[randint(0,len(t)-1)])
print unicode(connect)
And the output is :
Error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 0: ordinal not in range(128)
推荐答案
使用非ASCII字符时,指定源代码的编码在文件的顶部。然后,对所有文本使用Unicode字符串:
When using non-ASCII characters, specify the encoding of the source code at the top of the file. Then, use Unicode strings for all text:
#coding:utf8
from random import randint
p=[u'Bu', u'Şu']
k=[u'yazı', u'makale']
t=[u'hoş', u'ilgiç']
connect= u'%s %s %s'%(p[randint(0,len(p)-1)],k[randint(0,len(k)-1)],t[randint(0,len(t)-1)])
print connect
输出:
Şu yazı ilgiç
你可以如果您的执行环境不支持字符集,仍然可以获得 UnicodeEncodeError
。理想地使用支持UTF-8输出编码的环境。
You could still get UnicodeEncodeError
if your execution environment doesn't support the character set. Ideally use an environment that supports an output encoding of UTF-8.
这篇关于土耳其字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!