gpt4 book ai didi

python - list.append(obj) 导致回溯 - 这是为什么?

转载 作者:行者123 更新时间:2023-11-28 17:50:05 27 4
gpt4 key购买 nike

考虑以下代码:

for url in urls:
obj = HtmlInfo()

obj.url = url
obj.html = hc.get_html(url)
obj.tag_count = hc.get_num_tags(obj.html, 0, True)
htmlinfos.append(obj)

其中 urls 是一个 url 列表,而 htmlinfos 被初始化为一个空列表,就像这样 - 在循环之前,当然:

htmlinfos = [ ]

然而,无论出于何种原因,当我尝试运行此代码时,我都会遇到 list-assignment index out of range 异常。

这可能是什么问题?请注意,我的 Python 版本是 2.7,我使用的是最新的稳定版 Django(我相信是 1.4)

更新 - 追溯

Environment:


Request Method: GET
Request URL: http://xx.xxx.xxx.xx/xxx/0/test/

Django Version: 1.4.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'scrapper',
'django_pdb')
Installed Middleware:
('django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django_pdb.middleware.PdbMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/share/nginx/www/xxx/private/xxx/views.py" in test
44. return HttpResponse("Dis be er bad query yo " + test_id )
File "/usr/share/nginx/www/xxx/private/xxx/views.py" in __get_html_list
23. return list

Exception Type: IndexError at /xxx/0/test/
Exception Value: list assignment index out of range

更新 - __get_html_list()

def __get_html_list():
hc = HtmlCounter()

htmlinfos = [ ]

#add more urls here for testing
urls = [ '/usr/share/nginx/www/xxx/private/template/test/html_count_test.html' ]


for url in urls:
obj = HtmlInfo()

obj.url = url
obj.html = hc.get_html(url)
obj.tag_count = hc.get_num_tags(obj.html)
htmlinfos.append(obj)

return htmlinfos

注意

最初 htmlinfos 被简单地称为 list,所以我在发布之前更改了它,重新运行它,但我仍然遇到同样的错误:/

更新 - get_html_tag_count()

为了简洁起见,我想我也可以发布这个,以防这可能与问题有关:

def get_num_tags(self, html):

if reset:
self.reset()

current_index = 0

for char in html:

if (char == "<"):

close_index = html[current_index:].find("/>", current_index)

if close_index == -1:
break
else:
++self._tag_count

++current_index

return self._tag_count

最佳答案

尝试将 ++self._tag_count++current_index 替换为 self._tag_count+=1current_index+=1分别

++var 适用于大多数语言,但 python 与大多数语言不同。

关于python - list.append(obj) 导致回溯 - 这是为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11870488/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com