Turkish character encoding
21,042
First of all you should put at the top of your script # -*- coding: utf-8 -*-
to be able to use non-ascii characters in your script. Also while printing decode str to unicode will solve your problem.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from random import randint
p=['Bu', 'şu']
k=['yazı','makale']
t=['hoş','ilginç']
connect='%s %s %s'%(p[randint(0,len(p)-1)],k[randint(0,len(k)-1)],t[randint(0,len(t)-1)])
print connect.decode('utf-8')
Author by
Alkindus
Updated on October 04, 2020Comments
-
Alkindus over 3 years
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)
-
Alkindus over 9 yearsTurkish is not Latin-1. We use UTF-8 for encoding websites. Adding header doesn't effect result. Its necessary when you use Turkish Chars in coding.
-
Phani over 9 yearsI thought you did use the Turkish characters when assigning the lists
p
,k
andt
. -
Mark Tolonen almost 5 yearsIt works if you have a UTF-8 terminal. It won't work on typical Windows command prompt.