Reputation: 3
Having troubles parsing a list of dictionaries, accessing all key-value pairs per dictionary, through a loop in order to process each k-v at a time.
I have a json file that consists of a list of dictionaries. In simplified form:
[
{"": "0", "a": "3893", "b": "2389", "c": "1209"},
{"": "1", "a": "4308", "b": "4560", "c": "9127"},
...
]
I want to loop through this per dictionary, and for each key-value pair (excluding the first one, assuming it will be skipped because the key is empty) per iteration, having access to the a,b,c k-v pairs of one dict, so that they are individually processed.
I tried:
import json
with open("file.json", "r") as read_file:
data = json.load(read_file)
result_list = []
for d in data:
result_list.append([v for k,v in d.items()])
for k, v in enumerate(result_list.items()):
# process each key-value pair
But I get the following error:
Traceback (most recent call last):
File "somescript.py", line 28, in <module>
for k, v in enumerate(result_list.items()):
AttributeError: 'list' object has no attribute 'items'
Meanwhile I tried:
import json
with open("file.json", "r") as read_file:
data = json.load(read_file)
for i in data:
print(i, data[i])
And get:
Traceback (most recent call last):
File "somescript.py", line 26, in <module>
print(i, dat[i])
TypeError: list indices must be integers or slices, not dict
The code is faulty, I don't yet know how to achieve the above, it may be simple but I haven't found it yet.
Upvotes: 0
Views: 83
Reputation: 27515
You should be able to load the entire list without rebuilding it just using json.load
, then iterate over it and the dicts
import json
with open("file.json", "r") as read_file:
data = json.load(read_file)
for d in data:
for i, (k, v) in enumerate(d.items()):
print(i, k, v)
That said I suggest you save the file as a normal JSON file as it seems the first key/value in each dict is supposed to be an ID
field so it should look as follows:
{
"0": {
"a":"3893",
"b":"2389",
"c":"1209"
},
"1": {
"a":"4308",
"b":"4560",
"c":"9127"
}
}
Which you can do using a dict comprehension:
with open("file.json", 'w') as out_file:
json.dump({d.pop(""): d for d in data}, out_file, indent=4)
Then all you'd need to do is simply iterate over the dicts:
with open("file.json", "r") as read_file:
data = json.load(read_file)
for _id, item in data.items():
for k, v in item.items():
print(_id, k, v)
Upvotes: 0
Reputation: 83557
result_list = []
for d in data:
result_list.append([v for k,v in d.items()])
This creates a list of lists. Note that when you do this, you are throwing away the keys in your dictionaries. This probably isn't what you want.
for k, v in enumerate(result_list.items()):
# process each key-value pair
You get an error here because result_list
is a list, not a dictionary. Lists do not have a method named items()
.
I suggest instead that you iterate over data
directly. There is no need for result_list
:
for d in data:
for k, v in d.items():
# process each key-value pair
Upvotes: 0
Reputation: 423
The first part of your problem is in your enumerate call:
for k, v in enumerate(result_list): # take off the .items())
# process each key-value pair
You don't invoke .items() on the result_list; enumerate consumes it as a list.
Answering the second of your problems:
import json
with open("file.json", "r") as read_file:
data = json.load(read_file)
for i in data:
print(i, data[i])
Assuming that json is a list of dictionaries like you've been using, you'll need to:
for item in data:
for key in item:
print(key, item[key])
to account for double-dereferencing.
Upvotes: 1