0

我在尝试写入 Excel 工作表时低于 unicodedecodeerror。

异常类型:UnicodeDecodeError 异常值:
'ascii'编解码器无法解码位置 7 的字节 0xc3:序数不在范围内(128)无法编码/解码的字符串是:i>����R<

我的观点:

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() 

奇怪的是。File_write 函数运行良好,它将“ÖĞR”写入本地文本文件。但是当我尝试将“ÖĞR”写入excell workseeht时,它会引发错误。

我也试过 worksheet.write("A4", teachertitle.encode('utf-8'),titlescell) 但问题仍然存在。

我也有 # - - coding: utf-8 - - 在views.py的开头

4

2 回答 2

0

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()
于 2018-11-23T19:38:53.750 回答
0

最后,

解决方案是:

worksheet.write(“A4”,teachertitle.decode('utf-8'),titlescell)

解码解决了它。据我了解,Excel 工作簿需要在写入 Excel 工作表之前对字符串进行解码。

于 2018-11-29T14:27:59.713 回答