Reputation: 141
I have this code that adds 50 points to a user in my json file but I keep getting a 'dict' object has no attribute 'append'
when trying to append new users to the users:
def updateUsers(chan):
j = urllib2.urlopen('http://tmi.twitch.tv/group/user/' + chan + '/chatters')
j_obj = json.load(j)
with open('dat.dat', 'r') as data_file:
data = json.load(data_file)
for dat in data['users']:
if dat in j_obj['chatters']['moderators'] or j_obj['chatters']['viewers']:
data['users']['tryhard_cupcake']['Points'] += 50
else:
data['users'].append([dat]) # append doesn't work here
with open('dat.dat', 'w') as out_file:
json.dump(data, out_file)
What is the proper way of adding new objects/users to users
?
Upvotes: 6
Views: 47821
Reputation: 315
This error message has your answer.
https://docs.python.org/2/tutorial/datastructures.html#dictionaries
data['users'] = [dat]
If you want to append to the existing list.
templist = data['users']
templist.extend(dat)
data['users'] = templist
Upvotes: 11
Reputation: 139
It seems that data['users']
is a dictionary, so you can only use dictionary methods to add keys and values.
Upvotes: 2