2016-03-09 43 views
1

我有我的代码在这里这样的,我想更新列表内的一些项目:更新Python字典元素给人以[]

coordinates = mongo_query.get_items({}) 
for json in coordinates: 
    json["person"] = "test" 
    json["location"] = "test" 
    for j in range(0, len(json["types"])): 
     json["types"][j] = "test" 
new_coordinates = coordinates 

这里的时候,我调试变量new_coordinates是空的是这样的:'[]'

坐标的结果,如果我这样做json_util.dumps(coordinates)它给出了这样的:

coordinates= [{"name": "my name", "timestamp": {"$date": 1459002562091}, "longitude": 20.169550966746304, "location": "Work", "victim": {"language": "English", "locality": "Bern", "gender": "Other", "region": "Gabon", "birthday": {"$date": 506736000000}, "nationality": "United States", "ethnicity": "Bosnian"}, "person": "Stranger", "latitude": 43.05529651674635, "personGender": "Male", "types": ["Shouting"]}, {"name": "my name", "timestamp": {"$date": 1455632962091}, "longitude": 21.292620354706038, "location": "Public Space", "victim": {"language": "English", "locality": "Ferizaj", "gender": "Other", "region": "Kosovo", "birthday": {"$date": 601516800000}, "nationality": "Canada", "ethnicity": "Turkish"}, "person": "Waiter", "latitude": 42.81558228232729, "personGender": "Male", "types": ["Comments", "Whistling"]}] 

为什么会这样,我不明白为什么我不能更新Ë在坐标列表上放置什么,以及它为什么会给出一个空列表?有人能帮助我吗?

编辑:

所以看到的坐标更好地在这里:Jsonblob link

+0

你介意漂亮的印刷'coordinates'?目前阅读有点奇怪。 – erip

+0

@erip请检查我所做的修改,我添加了一个jsonblob链接,以便您可以更好地看到它? – EgzEst

+0

是的,谢谢。 – erip

回答

1

我已经重复你的代码,导入coordinates = <your .json file>,我是可以修改的坐标 - 只注意你错过拼写“的人“作为”peron“。

我会调查:

  • 你确定你不要做什么用毕竟坐标? 请注意,something = other_something与说something = copy(other_something)不一样。
  • 是在列表[坐标]内打印出的坐标变量。
  • 我建议你在一个python shell上重复我的过程:coordinates = <copy the content that you've output through that link>,并试试其中一个元素。这个工作对我来说:

    coordinates = # copy the stuff here [ {} ] 
    for json in coordinates: 
        json['person'] = 'bla' 
    new_coordinates = coordinates 
    new_coordinates 
    

    ,输出:[{'person': 'bla',...