I'm trying to use the Python wrapper for CouchDB to update a database. The file is structured as a nested dictionary as follows.

doc = { ...,
   'RLSoo': {'RT_freq': 2, 'tweet': "They're going to play monopoly now. 
     This makes me feel like an excellent mother. #Sandy #NYC"}, 
    'GiltCityNYC': {},
  ....}

I would like to put each entry of the larger dicitionary, for example RLSoo into its own document. However, I get an error message when I try the following code.

 for key in doc:
      db.update(doc[key],all_or_nothing=True)

Error Message

TypeError: expected dict, got <type 'str'>

I don't understand why CouchDB won't accept the dictionary.

有帮助吗?

解决方案

According Database.update() method realization and his documentation, first argument should be list of document objects (e.g. list of dicts). Since you doc variable has dict type, direct iteration over it actually iterates over all his keys which are string typed. If I understood your case right, probably your doc contains nested documents as values. So, try just:

db.update(doc.values(), all_or_nothing=True)

And it all first level values are dicts, it should works!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top