【问题标题】:"IndexError: list index out of range" while charging MySQL DB对 MySQL 数据库收费时出现“IndexError: list index out of range”
【发布时间】:2014-07-08 09:31:50
【问题描述】:

我在执行我的代码时收到以下错误代码。该错误不会立即发生 - 它会在 2-7 小时后随机发生。在错误发生之前,流式传输在线提要并将它们写入数据库是没有问题的。

错误信息:

Traceback (most recent call last):
File "C:\Python27\MySQL_finalversion\RSS_common_FV.py", line 78, in <module>
main()
File "C:\Python27\MySQL_finalversion\RSS_common_FV.py", line 63, in main
feed_iii = feed_load_iii(feed_url_iii)
File "C:\Python27\MySQL_finalversion\RSS_common_FV.py", line 44, in feed_load_iii
in feedparser.parse(feed_iii).entries]
IndexError: list index out of range

您可以在这里找到我的代码:

import feedparser
import MySQLdb
import time
from cookielib import CookieJar

db = MySQLdb.connect(host="localhost", # your host, usually localhost
                 user="root", # your username - SELECT * FROM mysql.user
                 passwd="****", # your password
                 db="sentimentanalysis_unicode",
                 charset="utf8") # name of the data base

cur = db.cursor()
cur.execute("SET NAMES utf8")
cur.execute("SET CHARACTER SET utf8")
cur.execute("SET character_set_connection=utf8")
cur.execute("DROP TABLE IF EXISTS feeddata_iii")

sql_iii = """CREATE TABLE feeddata_iii(III_ID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(III_ID),III_UnixTimesstamp integer,III_Timestamp varchar(255),III_Source varchar(255),III_Title varchar(255),III_Text TEXT,III_Link varchar(255),III_Epic varchar(255),III_CommentNr integer,III_Author varchar(255))"""

cur.execute(sql_iii)

def feed_load_iii(feed_iii):
return [(time.time(),
         entry.published,
         'iii',
         entry.title,
         entry.summary,
         entry.link,
         (entry.link.split('=cotn:')[1]).split('.L&id=')[0],
         (entry.link.split('.L&id=')[1]).split('&display=')[0],
         entry.author)
        for entry
        in feedparser.parse(feed_iii).entries]

def main():
feed_url_iii = "http://www.iii.co.uk/site_wide_discussions/site_wide_rss2.epl"

feed_iii = feed_load_iii(feed_url_iii)

print feed_iii[1][1]

for item in feed_iii:
    cur.execute("""INSERT INTO feeddata_iii(III_UnixTimesstamp, III_Timestamp, III_Source, III_Title, III_Text, III_Link, III_Epic, III_CommentNr, III_Author) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)""",item)
db.commit()

if __name__ == "__main__":
while True:
    main()
    time.sleep(240)

如果您需要更多信息 - 请随时询问。我需要你的帮助!

来自伦敦的感谢和问候!

【问题讨论】:

  • 首先你需要修正你的缩进,现在关于你的问题,你想访问一个不存在的列表中的单元格(参见你的splits),尝试打印或记录之前的条目执行代码,你会看到你的问题在哪里。
  • 我的理解是否正确:错误属于 feed_load_iii 块中的拆分功能?是否可以编写如下参数:try: split (X) except: set value 'NULL'
  • 这是你returndef feed_load_iii(feed_iii):的声明。
  • 你知道像 try: split (X) except: set value 'NULL' 这样的循环机会是否存在?
  • Yes 但不在列表理解中,另一种选择是使用生成器。

标签: python mysql feed


【解决方案1】:

本质上,您的程序对格式错误的数据的弹性不足。

您的代码对数据的结构做出了非常明确的假设,如果数据不是那么结构化,则无法应对。您需要检测数据格式不正确的情况,然后采取其他措施。

一种相当草率的方法会简单地捕获当前引发的异常,您可以使用它(类似的东西)

try:
    feed_iii = feed_load_iii(feed_url_iii)
except IndexError:
    # do something to report or handle the data format problem

【讨论】:

  • 你有什么想法#做一些事情来报告或处理数据格式问题 ...我对Python很陌生,不知道:(跨度>
  • 首先,报告它,而不是你可能会修复数据错误而不是忍受它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 2018-02-08
  • 2014-10-20
  • 1970-01-01
相关资源
最近更新 更多