The 'dict' object does not have the 'append' Json attribute

I have this code that adds 50 points to a user in my json file, but I keep getting 'dict' object has no attribute 'append'when I try to add new users to 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 correct way to add new objects / users in users?

+4
source share
2 answers

This error message has your answer.

https://docs.python.org/2/tutorial/datastructures.html#dictionaries

 data['users'] = [dat]

If you want to add to an existing list.

templist = data['users']
templist.extend(dat)
data['users'] = templist
+8
source

This seems to be data['users']a dictionary, so you can only use dictionary methods to add keys and values.

+1
source

Source: https://habr.com/ru/post/1615326/


All Articles