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...
line 13, in Reset
data[year].months.append({
AttributeError: 'dict' object has no attribute 'months'
The len() function does return an integer.
for x in y:
y here must be a list or something iterative not an integer
by using len(data)
your just returning the length of list
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;
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.
Instead you can do this
for item in data:
year = item['year']
or
for index in range(len(data)):
item = data[index]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.