json对象列表以浮点值对坐标

时间:2014-07-09 05:45:07

标签: python json

我正在阅读美国州立大学的json。 json格式:

"SD":[{"y":"-104.05","x":"45.94"},{"y":"-104.05","x":"43.00"},
{"y":"-98.49","x":"42.99"},{"y":"-98.44","x":"42.92"},{"y":"-98.19","x":"42.84"},
{"y":"-98.05","x":"42.77"},{"y":"-97.97","x":"42.76"},{"y":"-97.85","x":"42.85"},
{"y":"-97.22","x":"42.85"},{"y":"-97.20","x":"42.80"},{"y":"-96.84","x":"42.71"},
{"y":"-96.62","x":"42.52"},{"y":"-96.48","x":"42.47"},{"y":"-96.62","x":"42.72"},
{"y":"-96.50","x":"43.00"},{"y":"-96.43","x":"43.13"},{"y":"-96.55","x":"43.22"},
{"y":"-96.52","x":"43.38"},{"y":"-96.61","x":"43.48"},{"y":"-96.45","x":"43.49"},
{"y":"-96.45","x":"45.29"},{"y":"-96.74","x":"45.46"},{"y":"-96.84","x":"45.61"},
{"y":"-96.58","x":"45.82"},{"y":"-96.56","x":"45.93"},{"y":"-104.05","x":"45.94"}]}

加载json之后,它就成了:

[{u'y': u'-104.05', u'x': u'45.94'}, 
{u'y': u'-104.05', u'x': u'43.00'},
..., 
{u'y': u'-96.56', u'x': u'45.93'}, 
{u'y': u'-104.05', u'x': u'45.94'}]
temp = {u'y': u'-104.05', u'x': u'45.94'}

如何将其转换为:

[(-104.05,45.94), (-104.05,43.00),....., (-104.05,45.94)]

我尝试了不同的使用replace,format,map ..的值。但是,可以把它变成所需的格式吗?

请帮忙。

谢谢, 达斯

1 个答案:

答案 0 :(得分:0)

假设array =[{u'y': u'-104.05', u'x': u'45.94'}, {u'y': u'-104.05', u'x': u'43.00'},...,]

print [(float(item["y"]), float(item["x"])) for item in array]

<强>输出

[(-104.05, 45.94), (-104.05, 43.0), (-98.49, 42.99),...]