Python 'dict' 对象没有属性 'append' Json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33640689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:40:31  来源:igfitidea点击:

'dict' object has no attribute 'append' Json

pythonjson

提问by KekMeister Johhny

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:

我有这段代码为我的 json 文件中的用户添加了 50 个点,但是'dict' object has no attribute 'append'在尝试将新用户附加到用户时我不断收到:

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?

将新对象/用户添加到 的正确方法是users什么?

采纳答案by UglyCode

This error message has your answer.

此错误消息有您的答案。

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

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

回答by ham_string

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

好像data['users']是字典,所以只能用字典的方法来添加键和值。