'dict'对象没有属性'append'Json [英] 'dict' object has no attribute 'append' Json

查看:115
本文介绍了'dict'对象没有属性'append'Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,可以在json文件中为用户添加50点,但是在尝试向用户添加新用户时,我一直得到'dict' object has no attribute 'append':

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)

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

What is the proper way of adding new objects/users to users?

推荐答案

此错误消息有您的答案.

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

这篇关于'dict'对象没有属性'append'Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆