如何在 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)

更新於: 20-Feb-2020

25K+ 瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.