I have a dictionary, user_dict, and create a list for each user value, this list contains json object:
user_dict[user].append(obj)
Now I want to print all the items in this dictionary, but only select some field from each item in the list, so the dictionary is like this, I only list one user here
{u'user1':[{u'host_dst': {u'addr': u'195.149.144.60', u'vid': 0, u'port': 80},
'usi': '7932fee11ba72ae84180044d75521368', u'host_src': {u'addr': u'83.233.59.215',
u'vid': 0, u'port': 51068},item2...]}
What I did is:
for item in user_dict.values():
fd_out1.write("%s\t%s\n" % (item["host_dst"][addr],item["host_dst"]["vid"]))
and it returns:
TypeError: list indices must be integers, not str
Previously for example, I used obj[“host_dst”][addr] to represent the value 195.149.144.60, and it works fine, but here when I want to print out something, it cannot work. Can anyone help? Many thanks!!
you missed a bracket in your post
after proper formatting, your data looks like this
so each
itemin your loop is alistyou should print out something like
item[0]["host_dst"][addr]