如何在 Python 中讀取和寫入 unicode(UTF-8)檔案?


現在推薦使用 io 模組,並且與 Python 3 的 open 語法相容:以下程式碼用於在 Python 中讀取和寫入 unicode(UTF-8)檔案

示例

import io
with io.open(filename,'r',encoding='utf8') as f:
    text = f.read()
# process Unicode text
with io.open(filename,'w',encoding='utf8') as f:
    f.write(text)

更新時間:2020 年 2 月 20 日

26K+ 的訪問量

開啟你的 職業生涯

完成課程認證

開始
廣告
© . All rights reserved.