Turkish character encoding

I am trying to create a new offer from different list items. It gives an error when I print it to unicode. I can print it normally (no unicode). When I try to post it to the website, it has the same error. I have argued 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)

      

+5


source to share


4 answers


You should put a header like this at the top of your script and specify the encoding on your system. It is recommended that you read more about this, as you often encounter such problems. Some resources are here .

#!/usr/bin/env python
# -*- coding: latin-1 -*-

      



Be sure to replace the above "Latin-1" with a suitable one for you.

0


source


First of all, you must put at the top of your script # -*- coding: utf-8 -*-

to use non-ascii characters in your script. Also when printing decode str to unicode you 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')

      

0


source


>>> 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 connect.decode('utf-8')
Şu makale ilgiç

      

0


source


When using non-ASCII characters, include the source encoding 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

      

Output:

Şu yazı ilgiç

      

You can still get UnicodeEncodeError

it if your runtime doesn't support the character set. Ideally, use an environment that supports the output UTF-8 encoding.

0


source







All Articles