I have converted a csv file to a list of dictionaries.
csvfile = './xyz.csv'
jsonArray = []
with open(csvfile, encoding='utf_8_sig') as csvf:
csvReader = csv.DictReader(csvf)
for row in csvReader:
jsonArray.append(row)
When I print the result it looks like this (list of dicts):
[
{
'Meter-id': 'W000001',
'January': '7.20',
'February': '7.2010',
'March': '7.34',
'April': '7.4870',
},
{
'Meter-id': 'W000002',
'January': '301,069.00',
'February': '318,592.000',
'March': '331,158.00',
'April': '345,322.000',
},
{
'Meter-id': 'K000001',
'January': '767,410.80',
'February': '784,932.700',
'March': '797,636.90',
'April': '812,111.000',
},...
]
But when I try to use the Array to group the dictionaries but the array seems empty:
resp = defaultdict(list)
KEY_MAP = {
'W': 'Warm',
'K': "Cold"
}
for key, group in itertools.groupby(jsonArray, key=lambda x: KEY_MAP.get(x["Meter-id"][0])):
for thing in group:
resp[key].append(thing)
What am I doing wrong here?
Thank you in advance
A
Your code and your sample works well for me (except indentation). Maybe you can try a version without groupby
(especially if your json array is not sorted by group)
Update :
How can I fix
IndexError: string index out of range
Do not use [0]
but a slice like [:1]
to not raise an exception and use default
argument of dict.get
to return a default group:
resp = defaultdict(list)
for x in jsonArray:
group = KEY_MAP.get(x["Meter-id"][:1], 'Misc')
resp[group].append(x)
In each method, the output is:
defaultdict(list,
{'Warm': [{'Meter-id': 'W000001',
'January': '7.20',
'February': '7.2010',
'March': '7.34',
'April': '7.4870'},
{'Meter-id': 'W000002',
'January': '301,069.00',
'February': '318,592.000',
'March': '331,158.00',
'April': '345,322.000'}],
'Cold': [{'Meter-id': 'K000001',
'January': '767,410.80',
'February': '784,932.700',
'March': '797,636.90',
'April': '812,111.000'}]})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.