我對python相當陌生。我有一個我需要了解的錯誤。Python - 瞭解錯誤:IndexError:列表索引超出範圍
代碼:
config.py:
# Vou definir os feeds
feeds_updates = [{"feedurl": "http://aaa1.com/rss/punch.rss", "linktoourpage": "http://www.ha.com/fun.htm"},
{"feedurl": "http://aaa2.com/rss.xml", "linktoourpage": "http://www.ha.com/fun.htm"},
{"feedurl": "http://aaa3.com/Heaven", "linktoourpage": "http://www.ha.com/fun.htm"},
{"feedurl": "http://aaa4.com/feed.php", "linktoourpage": "http://www.ha.com/fun.htm"},
{"feedurl": "http://aaa5.com/index.php?format=feed&type=rss", "linktoourpage": "http://www.ha.com/fun.htm"},
{"feedurl": "http://aaa6.com/rss.xml", "linktoourpage": "http://www.ha.com/fun.htm"},
{"feedurl": "http://aaa7.com/?format=xml", "linktoourpage": "http://www.ha.com/fun.htm"},
{"feedurl": "http://aaa8/site/component/rsssyndicator/?feed_id=1", "linktoourpage": "http://www.ha.com/fun.htm"}]
twitterC.py
# -*- coding: utf-8 -*-
import config # Ficheiro de configuracao
import twitter
import random
import sqlite3
import time
import bitly_api #https://github.com/bitly/bitly-api-python
import feedparser
...
# Vou escolher um feed ao acaso
feed_a_enviar = random.choice(config.feeds_updates)
# Vou apanhar o conteudo do feed
d = feedparser.parse(feed_a_enviar["feedurl"])
# Vou definir quantos feeds quero ter no i
i = range(8)
print i
# Vou meter para "updates" 10 entradas do feed
updates = []
for i in range(8):
updates.append([{"url": feed_a_enviar["linktoourpage"], "msg": d.entries[i].title + ", "}])
# Vou escolher ums entrada ao acaso
print updates # p debug so
update_to_send = random.choice(updates)
print update_to_send # Para efeitos de debug
而這有時會出現由於隨機性質的錯誤:
Traceback (most recent call last):
File "C:\Users\anlopes\workspace\redes_sociais\src\twitterC.py", line 77, in <module>
updates.append([{"url": feed_a_enviar["linktoourpage"], "msg": d.entries[i].title + ", "}])
IndexError: list index out of range
I'am沒有得到的錯誤,列表「feeds_updates」是一個包含8個元素的列表,我認爲是很好的聲明,RANDOM會從8個列表中選擇一個...
有人可以給我一些線索,這裏? PS:對不起,我的英語不好。
此致
您知道索引從零開始? 8個元素的索引爲0到7? – 2011-04-01 10:09:27
@S。洛特:'範圍(8)'是'[0,1,2,3,4,5,6,7]',所以這不是問題。 – 2011-04-01 10:50:25
@Tim Pietzcker:當然,我們都假設在'feed_updates'列表中實際提供的代碼有8個項目,對嗎?是的,證據是好的,但必須有一些事情沒有在所提供的代碼片段中顯示。 – 2011-04-01 11:02:51