簡體   English   中英

使用 Python 將希伯來語寫入文本文件時出現編碼問題

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

Django 項目

編碼問題:代碼流

  1. 讀取 Excel 文件

  2. 操作來自 Excel 的數據

  3. 創建新的txt文件並寫入其中

  4. 將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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM