Reputation: 879
I get below unicodedecodeerror while trying to write to excell worksheet.
Exception Type: UnicodeDecodeError Exception Value:
'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128) The string that could not be encoded/decoded was: i>����R<
My view lines :
def file_write(input):
handle1=open('/tmp/filelog.txt','a')
handle1.write(str(input))
handle1.close()
workbook = xlsxwriter.Workbook('report.xlsx')
worksheet = workbook.add_worksheet()
teachertitle = "ÖĞR"
file_write(teachertitle)
worksheet.write("A4", teachertitle, titlescell)
workbook.close()
The strange thing is. File_write function is working well and it writes "ÖĞR" to a local text file. But when i try to write "ÖĞR" to excell workseeht it throws error.
I also tried worksheet.write("A4", teachertitle.encode('utf-8'), titlescell) but still problem continue.
I also have # -- coding: utf-8 -- at the beginning of views.py
Upvotes: 0
Views: 282
Reputation: 879
At last,
Solution is :
worksheet.write("A4", teachertitle.decode('utf-8'), titlescell)
Decoding solved it. As i understand excell workbook needs the string to be decoded before writing to excell sheet.
Upvotes: 0
Reputation: 15916
The problem is most likely with your file_write
function, where you need to set the encoding of the file to be able to handle utf-8
. In python3, you can do that using:
def file_write(input):
handle1=open('/tmp/filelog.txt','a', encoding='utf-8')
handle1.write(str(input))
handle1.close()
Upvotes: 0