0

I know this is going to sound like I just need to use json.loads from the title. But I don't think that's the case here. Or at least I should be able to do this without any libraries (plus I want to understand this and not just solve it with a library method).

What I have currently is a dictionary where the keys are words and the values are total counts for those words:

myDict = { "word1": 12, "word2": 18, "word3": 4, "word4": 45 }

and so on...

what I want is for it to become something like the following (so that I can insert it into a scraperwiki datastore):

myNewDict = {"entry": "word1", "count": 12, "entry": "word2", "count": 18, "entry": "word3", "count": 4, "entry": "word4", "count": 45}

I figured I could just loop over myDict and insert each key/value after my new chosen keys "entry" and "count" (like so):

for k, v in myDict.iteritems():
    myNewDict = { "entry": k, "count": v }

but with this, myNewDict is only saving the last result (so only giving me myNewDict={"entry":"word4", "total":45}

what am I missing here?

roy
  • 3,706
  • 6
  • 30
  • 53
  • See http://stackoverflow.com/questions/3721357/how-to-dump-a-python-dictionary-to-json-when-keys-are-non-trivial-objects and http://stackoverflow.com/questions/7100125/storing-python-dictionaries – Mihai8 Apr 01 '13 at 20:18
  • 1
    The keys on a dict (or a JSON object) must be unique, so the best you can do is a list of entry-count dicts, e.g. `[{"entry":"word1", "count": 12},{"entry":"word2", ...}, ...]` – Francis Avila Apr 01 '13 at 20:28

2 Answers2

2

What you need is a list:

entries = []

for k, v in myDict.iteritems():
   entries.append({ "entry": k, "count": v })

Or even better with list comprehensions:

entries = [{'entry': k, 'count': v} for k, v in myDict.iteritems()]

In more details, you were overriding myDict at each iteration of the loop, creating a new dict every time. In case you need it, you could can add key/values to a dict like this :

myDict['key'] = ...

.. but used inside a loop, this would override the previous value associated to 'key', hence the use of a list. In the same manner, if you type:

myNewDict = {"entry": "word1", "count": 12, "entry": "word2", "count": 18, "entry": "word3", "count": 4, "entry": "word4", "count": 45}

you actually get {'count': 45, 'entry': 'word4'} !

Note: I don't know what's the expected format of the output data but JSON-wise, a list of dicts should be correct (in JSON keys are unique too I believe).

Antoine Lassauzay
  • 1,557
  • 11
  • 12
  • 1
    Don't forget about list comprehension `[{'entry': k, 'count': v} for k, v in myDict.iteritems()]` – Bryan Apr 01 '13 at 20:36
  • An equally valid JSON-like object would be `{'entries': [{k:v} for k, v in myDict.iteritems()]}`. – martineau Apr 01 '13 at 21:12
0

While it's not clear 100% clear what output you want, if you want just a string in the format you've outlined above, you could modify your loop to be of the form:

myCustomFormat = '{'
for k, v in myDict.iteritems():
    myCustomFormat += '"entry": {0}, "count": {1},'.format(k, v)
# Use slice to trim off trailing comma
myCustomFormat = myCustomFormat[:-1] + '}'

That being said, this probably isn't what you want. As others have pointed out, the duplicative nature of the "keys" will make this somewhat difficult to parse.

ernie
  • 6,356
  • 23
  • 28