简体   繁体   English

使用 Python 将希伯来语写入文本文件时出现编码问题

[英]Encoding issue when writing Hebrew to text file, with Python

Django Project Django 项目

Encoding problem: The flow of code编码问题:代码流

  1. reading Excel file读取 Excel 文件

  2. manipulate the data from the Excel操作来自 Excel 的数据

  3. create new txt file and write into it创建新的txt文件并写入其中

  4. send the txt file to the client将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

every thing is working just fine but when I open the the file with ANSI Encoding all the Hebrew change into gibberish一切正常,但是当我用ANSI 编码打开文件时,所有希伯来语都变成了乱码

I have try to change the coding with every Hebrew option https://docs.python.org/2.4/lib/standard-encodings.html我尝试使用每个希伯来语选项更改编码https://docs.python.org/2.4/lib/standard-encodings.html

The Hebrew coding should be write with ASCII NEW CODE or ASCII WINDOWS TEXT, any ideas?希伯来语编码应该用 ASCII NEW CODE 或 ASCII WINDOWS TEXT 编写,有什么想法吗?

You need to change the mode to "rb" and remove the encoding parameter您需要将模式更改为“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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM