如何在JSON中对对象内部的对象进行排序?(使用Python 2.7)
我有以下代码,这是一个将交易历史从数字货币钱包导出到json文件的函数 我面临的问题有两个:如何在JSON中对对象内部的对象进行排序?(使用Python 2.7),python,json,encoding,utf-8,Python,Json,Encoding,Utf 8,我有以下代码,这是一个将交易历史从数字货币钱包导出到json文件的函数 我面临的问题有两个: 我想允许json文件用utf-8编写,因为属性“label”可以是utf-8字符,如果我不考虑这一点,它将在文件中显示为\u\u\u等。但是,无论编码/解码('utf-8')的组合和顺序如何,我都无法获得最终输出文件以打印到utf-8 我希望按照我在代码中编写的顺序对每个迭代进行排序。我试着从collection订购Dict,但它没有订购物品,所以日期先到,等等 任何关于如何使用utf-8将其打印到我的
#这一行是for循环迭代的最后一行
#事务列表,“针对列表中的每个项目”
wallet_history.append({“Date”:time_string,“TXHash”:tx_hash,“Label”:Label,“Confirmations”:
确认,“金额”:价值\字符串,“费用”:费用\字符串,“余额”:余额\字符串})
尝试:
history_str=json.dumps(
钱包\u历史记录,确保\u ascii=False,排序\u键=False,缩进=4)
除类型错误外:
QMessageBox.critical(
无、(“无法创建json”)、(“无法创建json”))
jsonfile.close()
删除(文件名)
返回
jsonfile.write(历史记录)
要使生成的JSON以UTF-8编码,请使用确保ascii=False
:
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
>>> import json
>>> json.dumps(u'привет!', ensure_ascii=False)
u'"\u043f\u0440\u0438\u0432\u0435\u0442!"'
>>> print json.dumps(u'привет!', ensure_ascii=False)
"привет!"
>>> with open('test', 'w') as f:
... f.write(json.dumps(u'привет!', ensure_ascii=False))
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-6: ordinal not in range(128)
>>> with open('test', 'w') as f:
... f.write(json.dumps(u'привет!'.encode('utf-8'), ensure_ascii=False))
...
>>> with open('test') as f:
... f.read()
...
'"\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82!"'
>>> print '"\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82!"'
"привет!"
>>>
Python 2.7.6(默认,2014年3月22日,22:59:56)
>>>导入json
>>>json.dumps(u'Пцццц!),确保_ascii=False)
u'“\u043f\u0440\u0438\u0432\u0435\u0442!”“
>>>打印json.dumps(u'Пцццц!),确保_ascii=False)
"привет!"
>>>打开('test','w')作为f:
... f、 写入(json.dumps(u'ППцццц!),确保_ascii=False))
...
回溯(最近一次呼叫最后一次):
文件“”,第2行,在
UnicodeEncodeError:“ascii”编解码器无法对位置1-6中的字符进行编码:序号不在范围内(128)
>>>打开('test','w')作为f:
... f、 写入(json.dumps(u'ППццццц!'.encode('utf-8'),确保_ascii=False))
...
>>>以开放(“测试”)作为f:
... f、 读()
...
“\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82!””
>>>打印“'\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82!””
"привет!"
>>>
至于第二个关于秩序的问题。这是不可能的(除了编写自己的序列化程序),或者没有意义:
当您将
wallet\u history
传递给json.dumps
时,它已经失去了顺序,因为它包含无序的字典。您需要确保两个json都没有转义字符,并且您将json输出写为unicode:
import codecs
import json
with codecs.open('tmp.json', 'w', encoding='utf-8') as f:
f.write(json.dumps({u'hello' : u'привет!'}, ensure_ascii=False) + '\n')
$ cat tmp.json
{"hello": "привет!"}
至于您的第二个问题:您可以使用collections.OrderedDict
,但您需要小心地将其直接传递到json.dumps
,而不将其更改为简单dict。请参见区别:
from collections import OrderedDict
data = OrderedDict(zip(('first', 'second', 'last'), (1, 10, 3)))
print json.dumps(dict(data)) # {"second": 10, "last": 3, "first": 1}
print json.dumps(data) # {"first": 1, "second": 10, "last": 3}
请仅张贴与本文件相关的部分代码problem@thefourtheye完成!谢谢你能看到的提醒,我确实确保了ascii=False,而且它仍然没有用utf-8写。。。我想可能是因为我先将转储存储到字符串中?这与file.write()函数有关吗?…它在这里工作(请参阅我的更新)。您如何检查它是否仍然没有以utf-8写入?我通过在设置为utf8编码的文本编辑器中打开该文件进行检查。只有标签是用/u字节写的。您如何打开文件
jsonfile
?为什么要将它作为第二个参数传递给json.dumps
?另外,您确定标签
在传递给wallet\u history.append时尚未进行ASCII编码吗?