I have the following program which reads from a tsv file and imports to a dictionary. I need to sort the dictionary based on jobID and its giving me this error:
AttributeError: 'dict' object has no attribute 'jobid'
apps = {}
for row in csvreader:
apps[counter]= {'userid': row[0], 'time': row[1], 'jobid': row[2]}
counter = counter + 1
sorted_keys = sorted(apps.keys(), key=lambda x:apps.jobid)
Any an all help is appreciated.
sample output for the first three rows in the dict:
{'jobid': 'JobID', 'userid': 'UserID', 'time': 'ApplicationDate'}
{'jobid': '169528', 'userid': '47', 'time': '2012-04-04 15:56:23.537'}
{'jobid': '284009', 'userid': '47', 'time': '2012-04-06 01:03:00.003'}
Thank You!
看来您想使用apps[x]["jobid"]
而不是apps.jobid
。
You should use a list for apps. Then you can sort on jobid by using the dictionary lookup syntax x['jobid']. Here it is working with sample data:
csvreader = (
('user1', '2pm', 2),
('user2', '2pm', 3),
('user3', '2pm', 1)
)
apps = []
for row in csvreader:
apps.append({'userid': row[0], 'time': row[1], 'jobid': row[2]})
sorted_data = sorted(apps, key=lambda x:x['jobid'])
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.