简体   繁体   English

我有这个错误,我不知道该怎么办

[英]I have this error and I don't know what to do

def Reset():
        data = []
        data.append({
            'year': now.year,
            'months': []
        })
        for year in range(len(data)):
            for month in range(12):
                data[year].months.append({
                    'month': (month + 1),
                    'days': []
                })
                if (month+1 == 2):
                    daysAmount = 28
                    if (isinstance(year/4, int)):
                        daysAmount = 29
                elif (month+1 == 12):
                    daysAmount = 31
                elif (isinstance((month+1)/2, int)):
                    daysAmount = 30
                else:
                    daysAmount = 31
                for day in range(daysAmount):
                    data[year].months[month].days.append({
                        'day': (day + 1),
                    })
        with open('./data.txt', 'w') as outfile:
            json.dump(data, outfile)
    
    
    
    Reset()

I changed the code, but now another error came I don't understand.我更改了代码,但现在又出现了一个我不明白的错误。 What should I do about this one?我该怎么办? As you can see it does have an attribute months...如您所见,它确实有一个属性months ...

line 13, in Reset
    data[year].months.append({
AttributeError: 'dict' object has no attribute 'months'

The len() function does return an integer. len() function 确实返回 integer。

for x in y:

y here must be a list or something iterative not an integer y 这里必须是一个列表或迭代的东西而不是 integer

by using len(data) your just returning the length of list通过使用len(data)你只返回列表的长度

you should use你应该使用

for year in data:

Yes, there is a way to get the number of items in the data list instead of the actual objects into the 'year' variable and for that you should use是的,有一种方法可以将数据列表中的项目数而不是实际对象放入“年份”变量中,为此您应该使用

for year in range(len(data)): 

as follows如下

import json
import datetime
now = datetime.datetime.now()

def Reset():
    data = []
    data.append({
        'year': now.year,
        'months': []
    })
    for year in range(len(data)):
        for month in range(12):
            data[year]['months'].append({
                'month': (month + 1),
                'days': []
            })
            if (month+1 == 2):
                daysAmount = 28
                if (isinstance(year/4, int)):
                    daysAmount = 29
            elif (month+1 == 12):
                daysAmount = 31
            elif (isinstance((month+1)/2, int)):
                daysAmount = 30
            else:
                daysAmount = 31
            for day in range(daysAmount):
                data[year]['months'][month].days.append({
                    'day': (day + 1),
                })
    with open('./data.txt', 'w') as outfile:
        json.dump(data, outfile)



Reset()

The other way would be to use另一种方法是使用

for year in data:

as follows如下

import json
import datetime
now = datetime.datetime.now()

def Reset():
    data = []
    data.append({
        'year': now.year,
        'months': []
    })
    for year in data:
        for month in range(12):
            year['months'].append({
                'month': (month + 1),
                'days': []
            })
            if (month+1 == 2):
                daysAmount = 28
                if (isinstance(year/4, int)):
                    daysAmount = 29
            elif (month+1 == 12):
                daysAmount = 31
            elif (isinstance((month+1)/2, int)):
                daysAmount = 30
            else:
                daysAmount = 31
            for day in range(daysAmount):
                year['months'][month].days.append({
                    'day': (day + 1),
                })
    with open('./data.txt', 'w') as outfile:
        json.dump(data, outfile)

Reset()

when u do len(data), it returns an integer which is not iterable;当你做 len(data) 时,它返回一个不可迭代的 integer;

hence you should do something like:因此,您应该执行以下操作:

for year in range(len(data)) 

or或者

for item in data.item:
     year = item['year'] 
     ...

You are getting an error because you of this line您收到错误是因为您属于这一行

for year in len(data):

This len(data) returns integer and that is not iterable.此 len(data) 返回 integer 并且不可迭代。

Instead you can do this相反,您可以这样做

for item in data:
    year = item['year']

or或者

for index in range(len(data)):
    item = data[index]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM