1. 程式人生 > >python輸出文件為utf-8格式的文件

python輸出文件為utf-8格式的文件

py不同版本輸出json檔案,對檔案編碼的引數設定是不一樣的,在3版本中,若是要輸出帶中文的json文件,需要把輸出的編碼方式定義在open上,否則會引起輸出的json文件亂碼,具體的可以看例項:

# coding= utf-8
import json
outputFilePath = "/Users/suxiahua/Desktop/1111.json"
jsonStr =json.dumps({'text':"福建歡迎"},ensure_ascii=False,indent=2)
with open(outputFilePath, 'wt', encoding='utf-8') as
f: f.write(jsonStr) print(jsonStr)