iT邦幫忙

0

python 輸出txt 亂碼

  • 分享至 

  • xImage

大家好 我現在正在將input_json(json形式)輸出到txt檔案 但是輸出成功後的txt檔案內的中文一直是亂碼 如下
"content":"\u529f\u80fd\uff0c\u6a5f\u80fd\uff0c\u74b0\u4fdd\u3002 "
有參考網路上的做法加入encoding="utf-8"和ensure_ascii=False
但還是沒有解決 請問還可以怎麼下手

current_datetime = datetime.datetime.now()
formatted_datetime = current_datetime.strftime("%Y%m%d_%H%M%S")

# 構建檔案名稱
filename = f"{input_json.get('uuid')}_{formatted_datetime}.txt"
        
# 將input_json輸出到txt檔案
with open(filename, "w", encoding="utf-8") as file:
    json.dump(input_json, file,ensure_ascii=False)

            
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
pickuse
iT邦新手 4 級 ‧ 2023-08-16 09:41:50
最佳解答

通常你這個問題是 json 將中文編碼了沒錯,我剛剛稍微測試了一下,先附上程式碼:

import json

data = {"content": "這裡有一些可愛的中文字"}

with open("output.txt", 'w', encoding='utf-8') as f:
    f.write(json.dumps(data, ensure_ascii=False))

以及上述程式碼的執行結果:

{"content": "這裡有一些可愛的中文字"}

當你把 ensure_ascii=False 拿掉,可以造成你所敘述的問題,也就是被編碼了,他會輸出:

{"content": "\u9019\u88e1\u6709\u4e00\u4e9b\u53ef\u611b\u7684\u4e2d\u6587\u5b57"}

所以我會先建議你檢查一下你的程式碼是不是有其他地方在作怪,
或是你可以提供「更完整的程式碼樣本」,以方便大家幫你找問題。

看更多先前的回應...收起先前的回應...
haoze iT邦新手 5 級 ‧ 2023-08-16 09:52:43 檢舉

目前是在做一個flask應用

response = jsonify(input_json)
        response.headers["Content-Disposition"] = "attachment; filename="+filename
        return response, 200

也是會回傳正常的

{
    "data": [
        {
            "content": "功能,機能,環保。 ",
            "id": "1",
            "name": "A",
            "tag": "圓盤機,專利機,貼條機,機能針,空壓機,結合機,縫紉機,花邊機,針織機,環保纖維"
        },
        {
            "content ": "長短纖/平針織機能性用布。",
            "id": "2",
            "name": "B",
            "tag": ""
        },
        {
            "content ": "數位印花直噴機。",
            "id": "3",
            "name": "B",
            "tag": ""
        },
        {
            "content ": "熱昇華印表機,熱昇華墨水和轉印紙,義大利熱轉印機,棉T直噴機,布匹直噴機,領標機,免挑膠膜機。",
            "id": "4",
            "name": "B",
            "tag": ""
        },
        {
            "content ": "吊牌類,印標,洗標,尺寸標,布標,反光標,立體標,TPU熱轉印標,織標,橡膠&矽膠標,包裝類。",
            "id": "5",
            "name": "B",
            "tag": ""
        }
    ],
    "dictionary": "上膠,主軸,乳液,乳膠,五趾襪,五金",
    "statistics": [
        {
            "count": 1,
            "tag": "圓盤機"
        },
        {
            "count": 1,
            "tag": "專利機"
        },
        {
            "count": 1,
            "tag": "貼條機"
        },
        {
            "count": 1,
            "tag": "機能針"
        },
        {
            "count": 1,
            "tag": "空壓機"
        },
        {
            "count": 1,
            "tag": "結合機"
        },
        {
            "count": 1,
            "tag": "縫紉機"
        },
        {
            "count": 1,
            "tag": "花邊機"
        },
        {
            "count": 1,
            "tag": "針織機"
        },
        {
            "count": 1,
            "tag": "環保纖維"
        }
    ],
    "uuid": "123456789"
}

但不知道為甚麼輸出到txt或是json就會出錯@@

pickuse iT邦新手 4 級 ‧ 2023-08-16 09:57:12 檢舉

那需要看你「輸出至txt」的程式碼是怎麼寫的

haoze iT邦新手 5 級 ‧ 2023-08-16 10:03:14 檢舉

感謝您 現在成功了
我把

with open(filename, "w", encoding="utf-8") as file:
    json.dump(input_json, file,ensure_ascii=False)

改成

with open(filename, "w", encoding="utf-8") as file:
    file.write(json.dumps(input_json,ensure_ascii=False))
pickuse iT邦新手 4 級 ‧ 2023-08-16 10:14:26 檢舉

恭喜你,那如果沒問題且有幫助到你的話請標記為解答~

haoze iT邦新手 5 級 ‧ 2023-08-16 10:19:01 檢舉

更正 最後改為以下

response = make_response(json.dumps(input_json, ensure_ascii=False))
        response.headers["Content-Disposition"] = f"attachment; filename={filename.encode('utf-8').decode('latin-1')}"
        response.headers["Content-Type"] = "application/octet-stream; charset=UTF-8"

我要發表回答

立即登入回答