将希伯来语写入文本文件时出现编码问题,Python
Encoding issue when writing Hebrew to text file, with Python
Django 项目
编码问题:
代码流程
正在阅读Excel文件
处理来自 Excel
的数据
创建新的 txt 文件并写入其中
发送txt文件给客户端
coding = "utf8"
file = open(filename, "w", encoding=coding, errors="ignore")
for row in excel_data_df.iloc():
line = manipulate(row)
file.write(line)
file.close()
file_data = open(filename, "r", encoding=coding, errors="ignore")
response = HttpResponse(file_data, content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=' + filename
一切正常,但是当我用 ANSI 编码 打开文件时,所有希伯来语都变成了乱码
我尝试更改每个希伯来语选项的编码https://docs.python.org/2.4/lib/standard-encodings.html
希伯来语编码应该用 ASCII NEW CODE 或 ASCII WINDOWS TEXT 来写,
有什么想法吗?
您需要将模式更改为“rb”并移除编码参数
file = open(filename, "w")
for row in excel_data_df.iloc():
line = manipulate(row)
file.write(line)
file.close()
file_data = open(filename, "rb")
response = HttpResponse(file_data, content_type='application/vnd.ms-
excel')
response['Content-Disposition'] = 'attachment; filename=' + filename
Django 项目
编码问题: 代码流程
正在阅读Excel文件
处理来自 Excel
的数据创建新的 txt 文件并写入其中
发送txt文件给客户端
coding = "utf8" file = open(filename, "w", encoding=coding, errors="ignore") for row in excel_data_df.iloc(): line = manipulate(row) file.write(line) file.close() file_data = open(filename, "r", encoding=coding, errors="ignore") response = HttpResponse(file_data, content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename=' + filename
一切正常,但是当我用 ANSI 编码 打开文件时,所有希伯来语都变成了乱码
我尝试更改每个希伯来语选项的编码https://docs.python.org/2.4/lib/standard-encodings.html
希伯来语编码应该用 ASCII NEW CODE 或 ASCII WINDOWS TEXT 来写, 有什么想法吗?
您需要将模式更改为“rb”并移除编码参数
file = open(filename, "w")
for row in excel_data_df.iloc():
line = manipulate(row)
file.write(line)
file.close()
file_data = open(filename, "rb")
response = HttpResponse(file_data, content_type='application/vnd.ms-
excel')
response['Content-Disposition'] = 'attachment; filename=' + filename