python-3.x - /new-watch-hour-graph/ 处的 IndexError
问题描述
def getNewWatchedCountGraph(requests):
data = Video.getNewWatchedCountGraph(requests)
data = json.loads(data)
# print(data)
x = []
m = []
bg = {}
res = {}
monthnumbers = []
currentMonth = datetime.datetime.now().month
for item in data:
seconds = int(item['count'])
x.append(seconds)
mydate = datetime.datetime.strptime(item['_id'], "%Y-%m")
monthnumbers.append(mydate.month)
m.append(mydate.strftime("%B"))
startMonths = monthnumbers[0] #line 116
endMonths = currentMonth+1
data = []
mon = []
for months in range(startMonths,endMonths):
if months not in monthnumbers:
mon.append(calendar.month_name[months])
data.append(0)
else:
mon.append(calendar.month_name[months])
monthIndex = monthnumbers.index(months)
data.append(x[monthIndex])
res['series_name'] = "Views"
res['series'] = list(data)
res['xdata'] = list(mon)
restrn_response = dumps(res)
return HttpResponse(restrn_response)
我制作了这个函数来显示视图总数的图表。它在我的本地服务器上运行良好。但是在第 116 行的主服务器中显示列表索引超出范围。我在哪里做错了?
解决方案
发生这种情况是因为monthnumbers
是空的。鉴于它在迭代时被填充data
,我认为循环甚至没有开始,因为data
它是空的。
推荐阅读
- python - matplotlib 和 opencv typerror imagedata 无法转换为浮点数
- gradle - 我可以从 Gradle 自定义任务中执行 SonarQube 吗?
- reactjs - 无法读取未定义的属性“publicRuntimeConfig”
- c++ - 为什么在传递给函数而不是构造函数时调用析构函数?
- arrays - 将数据附加到 Swift 中的全局数组
- r - 如何建立与文件的连接?
- python - 如何对齐大脑 MRI
- python - 在 pandas.Series 中创建一个转变
- python - Python:`logger.info` 和 `logging.info` 有什么区别?
- java - MySQL 和 Java (JDBC) 连接错误:用户访问被拒绝