2016-03-09 75 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',...