1. 程式人生 > >python 字典中有中文,寫入檔案變為編碼的解決方法

python 字典中有中文,寫入檔案變為編碼的解決方法

 字典中有中文,寫入檔案變為編碼的解決方法

#!/usr/bin/python
# encoding:utf-8

import json


def writeDict(data):
    with open("data.txt", "w") as f:
    	f.write(json.dumps(data, ensure_ascii=False))

if __name__ == '__main__':

    dict_1 = {"北京": "BJP", "北京北": "VAP", "北京南": "VNP", "北京東": "BOP", "北京西": "BXP"}

    writeDict(dict_1)