My code is about getting names of some users and some points about them and write them into a text file as table. For example:
19(someSpace*)|حمید
20(someSpace*)|وحید
70(someSpace*)|خلیل
14(someSpace*)|Hamid
def roww(STR, n):
if n >= 10:
return str(n) +4*" " +"| " + STR + "\n"
else:
return str(n) +5*" " +"|" + STR + "\n"
def my_table(STR, m):
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import codecs as D
f = D.open(STR + '.txt', "w", encoding='utf-8')
i = 0
while(i < m):
i +=1
a = raw_input("Name: ").encode('utf-8')
b = raw_input("Grade: ")
f.write(roww(a,b))
f.close()
when execute:
my_table("grade",3)
Name: حمید
I get this error:
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-10-a48ceb393d9a> in <module>()
----> 1 my_table("grade",3)
<ipython-input-9-6a83996822a3> in my_table(STR, m)
14 while(i < m):
15 i +=1
---> 16 a = raw_input("Name: ").encode('utf-8')
17 b = raw_input("Grade")
18 f.write(roww(a,b))
C:\Users\Hamid\Anaconda2\lib\encodings\utf_8.pyc in decode(input, errors)
14
15 def decode(input, errors='strict'):
---> 16 return codecs.utf_8_decode(input, errors, True)
17
18 class IncrementalEncoder(codecs.IncrementalEncoder):
UnicodeDecodeError: 'utf8' codec can't decode byte 0xcd in position 0: invalid continuation byte
I can't solve my problem with python about utf-8. Also I can't find any useful answer.