【发布时间】:2019-08-11 12:06:55
【问题描述】:
我正在尝试保存一个包含 áàã 等字符的文本文件。但是,我目前失败了,无法使用我在互联网上看到的内容。
我当前的代码是:
# -*- coding: utf-8 -*-
import codecs
import json
test_dict = {'name': [u'Joe', u'Doe'], 'id': u'1:2:3', 'description': u'he w\xe1','fav': [1, 2]}
final_text = line = "- " + json.dumps(test_dict) + "\n"
filename = 'C:\Users\PLUX\Desktop\data.txt'
f = codecs.open(filename,'w','utf8')
f.write(line)
哪个输出:
- {"description": "he w\u00e1", "fav": [1, 2], "name": ["Joe", "Doe"], "id": "1:2:3"}
我希望它输出:
- {"description": "he wá", "fav": [1, 2], "name": ["Joe", "Doe"], "id": "1:2:3"}
谁能帮帮我?
【问题讨论】:
-
如果你可以使用 Python 3
-
不幸的是没有。我只能使用 Python 2.7。非常感谢您的评论
标签: python json python-2.7 file text