我有一个python对象,我将其转储到json并写入一个文件。
results = [
{'destination': (x,y), 'id': 'dsss', 'origin': (x,r), 'waypoints': [[s,l],[d,s]]},
{'destination': (x1, y1), 'id': 'ddsdsee', 'origin': (z,f), 'waypoints': [[e,y],[d,e]]}]
with gzip.open("test.json.gz",'w') as outfile:
json.dump(results,outfile,indent=2)然后,我在其他地方打开该文件,通过:
schedule_f = gzip.open("test.json.gz")
schedule = json.load(schedule_f)pprint(schedule[0])返回:
{'destination': [x,y], 'id': 'dsss', 'origin': [x,r], 'waypoints': [[s,l],[d,s]]为什么origin和destination字段转换为列表?我清楚地指定了(而不是[
发布于 2016-12-05 08:58:26
JSON没有任何元组的概念:只有数组,它可以映射到Python列表。
从实践的角度来看,这是没有区别的,但是如果你认为你真的需要元组,你必须自己去转换它们。
发布于 2016-12-05 08:58:08
嵌套Python对象可能比允许以JSON格式存储的复杂。JSON格式只有一个容器,当您将它导入Python时,它被解析为list。
这种格式转换不是保守的,它们破坏了信息。您也将无法存储datetime,它将被转换为字符串。
发布于 2016-12-05 08:58:18
JSON不支持元组,因此json模块将它们转换为由JSON支持的数组。
https://stackoverflow.com/questions/40970408
复制相似问题