Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从python dict更新geoJSON文件_Python_Dictionary_Geojson - Fatal编程技术网

从python dict更新geoJSON文件

从python dict更新geoJSON文件,python,dictionary,geojson,Python,Dictionary,Geojson,我有一个很大的geoJSON文件,它提供了一个选举地图。我已经抓取了一个站点,并将选民选区结果返回到一个python字典中,该字典如下:{u'605':[u'56',u'31'],u'602':[u'43',u'77'],等等。}其中键是选区编号,值列表是第一位候选人的选票和第二位候选人的选票 我想用字典中的结果更新我的geoJSON文件——它是所有选民选区的文件。在我的geoJSON文件中,我将辖区号作为键/值对之一(比如-“precNum”:602)。如何使用字典中的结果更新每个形状 我可以

我有一个很大的geoJSON文件,它提供了一个选举地图。我已经抓取了一个站点,并将选民选区结果返回到一个python字典中,该字典如下:
{u'605':[u'56',u'31'],u'602':[u'43',u'77']
,等等。}其中键是选区编号,值列表是第一位候选人的选票和第二位候选人的选票

我想用字典中的结果更新我的geoJSON文件——它是所有选民选区的文件。在我的geoJSON文件中,我将辖区号作为键/值对之一(比如-
“precNum”:602
)。如何使用字典中的结果更新每个形状

我可以使用如下内容来定位和循环geoJSON文件:

for precincts in map_data["features"]:
    placeVariable = precincts["properties"]

    placeVariable["precNum"] 
    #This gives me the precinct number of the current shape I am in.

    placeVariable["cand1"] = ?? 
    # I want to add the Value of first candidate's vote here

    placevariable["cand2"] = ?? 
    # I want to add the Value of second candidate's vote here

任何想法都会大有帮助

你的问题措辞混乱。 你需要更好地识别你的VAR

听起来你在试图累积选票总数。 因此,您需要:

  • 第100选区:[1200]
  • 101选区:[4300]
加入:

  • [5500]
假定累加器阵列为accum。当您刚刚添加以下内容时,您可以丢弃有关哪些选票来自何处的信息:

for vals in map_data['features'].values():
    while len(accum) < len(vals):
        accum.append(0)
    for i in range(len(vals)):
        accum[i] += vals[i]
地图数据['features']中VAL的
。值()
而len(累计)
下面是一个示例程序,证明了该解决方案:

>>> x = { 'z': [2, 10, 200], 'y' : [3, 7], 'b' : [4, 8, 8, 10 ] }
>>> accum = []
>>> for v in x.values():
...   while len(accum) < len(v):
...     accum.append(0)
...   for i in range(len(v)):
...     accum[i] += v[i]
... 
>>> accum
[9, 25, 208, 10]
>>> 
>x={'z':[2,10200],'y':[3,7],'b':[4,8,8,10]}
>>>累计=[]
>>>对于x.values()中的v:
...   而len(累积)>>累积
[9, 25, 208, 10]
>>> 

您可以这样更新它

your_dict = {u'605': [u'56', u'31'], u'602': [u'43', u'77']}

for precincts in map_data["features"]:

    placeVariable = precincts["properties"]
    prec = placeVariable["precNum"] 

    if your_dict.get(prec): #checks if prec exists in your_dict
        placeVariable["cand1"] = your_dict['prec'][0]
        placevariable["cand2"] = your_dict['prec'][0]

你能发布你的json文件的样本吗?当然。每个选区都是这样一个对象:
{“类型”:“特征”,“id”:0,“属性”:{“preNum”:40,“形状区域”:0.0},“几何体”:{“类型”:“多边形”,“坐标”:[[[[Coords here]]]]}
请参阅我的答案。这就是你想做的吗@Jonnyd怎么办@shshank谢谢!对不起,凯文,这不是我想要做的。我想用字典中的信息更新geoJSON文件中的信息。如果我的geoJSON中的形状对象看起来像这样:
{“type”:“Feature”,“id”:0,“properties”:{“preNum”:40,“shape_AREA”:0.0},“geometry”:{“type”:“Polygon”,“Coords”:[[[Coords here]]]}
我希望我的字典中位于辖区40的数据输入到该形状对象中,将属性更改为
“properties”:“婚前协议”:40,“形状面积”:0.0,“cand1”:70,“cand2”:14}