python-json以元组为键序列化字典
Python中是否有一种方法可以序列化使用元组作为键的字典:
a={(1,2):'a'}
只需使用json.dumps(a),就会产生:
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.6/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.6/json/encoder.py", line 367, in encode
chunks = list(self.iterencode(o))
File "/usr/lib/python2.6/json/encoder.py", line 309, in _iterencode
for chunk in self._iterencode_dict(o, markers):
File "/usr/lib/python2.6/json/encoder.py", line 268, in _iterencode_dict
raise TypeError("key {0!r} is not a string".format(key))
TypeError: key (1, 2) is not a string
Roberto asked 2020-01-06T00:03:46Z
6个解决方案
31 votes
您不能将其序列化为json,json对于什么算作dict键要比python灵活得多。
您可以将映射转换为键,值对的序列,如下所示:
>>> import json
>>> def remap_keys(mapping):
... return [{'key':k, 'value': v} for k, v in mapping.iteritems()]
...
>>> json.dumps(remap_keys({(1, 2): 'foo'}))
'[{"value": "foo", "key": [1, 2]}]'
SingleNegationElimination answered 2020-01-06T00:04:09Z
9 votes
JSON仅支持将字符串作为键。 您需要选择一种将这些元组表示为字符串的方法。
Ned Batchelder answered 2020-01-06T00:04:29Z
7 votes
您可以只使用str((1,2))作为键,因为json只希望将键作为字符串,但是如果使用此键,则必须使用a[str((1,2))]来获取值。
Dhiraj Thakur answered 2020-01-06T00:04:49Z
6 votes
from json import load, dump
from ast import literal_eval
x={ (0,1):'la-la la', (0,2):'extricate' }
# save: convert each tuple key to a string before saving as json object
with open('/tmp/test', 'w') as f: dump({str(k):v for k, v in x.items()}, f)
# load in two stages:#
# (i) load json object
with open('/tmp/test', 'r') as f: obj = load(f)
# (ii) convert loaded keys from string back to tuple
d={literal_eval(k):v for k, v in obj.items()}
参见:[https://stackoverflow.com/a/12337657/2455413]
markling answered 2020-01-06T00:05:09Z
2 votes
这是一种方法。 在解码主字典并重新排序整个字典之后,将要求对密钥进行json解码,但这是可行的:
import json
def jsonEncodeTupleKeyDict(data):
ndict = dict()
# creates new dictionary with the original tuple converted to json string
for key,value in data.iteritems():
nkey = json.dumps(key)
ndict[nkey] = value
# now encode the new dictionary and return that
return json.dumps(ndict)
def main():
tdict = dict()
for i in range(10):
key = (i,"data",5*i)
tdict[key] = i*i
try:
print json.dumps(tdict)
except TypeError,e:
print "JSON Encode Failed!",e
print jsonEncodeTupleKeyDict(tdict)
if __name__ == '__main__':
main()
我不主张这种方法的任何效率。 我需要用它来保存一些操纵杆映射数据到文件。 我想使用可以创建半人类可读格式的东西,以便在需要时可以对其进行编辑。
Demolishun answered 2020-01-06T00:05:34Z
2 votes
json只能接受字符串作为字典的键,您可以做的是用这样的字符串替换元组键
with open("file", "w") as f:
k = dic.keys()
v = dic.values()
k1 = [str(i) for i in k]
json.dump(json.dumps(dict(zip(*[k1,v]))),f)
而且,当您要阅读它时,可以使用以下方法将键更改回元组
with open("file", r) as f:
data = json.load(f)
dic = json.loads(data)
k = dic.keys()
v = dic.values()
k1 = [eval(i) for i in k]
return dict(zip(*[k1,v]))
thebeancounter answered 2020-01-06T00:05:58Z