IndexError:列表索引超出范围?
问题描述:
我有一个问题,我需要大家的帮助。 我读rar文件(100mb)并处理文本文件(包含在rarfile中)。IndexError:列表索引超出范围?
import glob
import os
import UnRAR2
from os import path, access, R_OK
os.chdir("E:\\sms")
for file in glob.glob("*.rar"):
# extract test.txt to memory
entries = UnRAR2.RarFile(file).read_files('*.txt')
test_content = entries[0][1]
#print test_content
for line in test_content.split("\n"):
A=line.split(' ')
print A[1]
结果:
19009057
7030
9119
9119
....
....
bla...bla...
......
9119
9119
9119
7050
9119
Traceback (most recent call last):
File "E:\LAPTRINH\Android\adt-bundle-windows\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydevd.py", line 1397, in <module>
debugger.run(setup['file'], None, None)
File "E:\LAPTRINH\Android\adt-bundle-windows\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydevd.py", line 1090, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "C:\Users\The\Documents\workspace\unrar\test_unrar.py", line 13, in <module>
print A[1]
IndexError: list index out of range
请帮帮我! 谢谢!!!
答
您的一行(可能是您的最后一行)不符合您的期望格式。为此在你内心的循环:
A=line.split(' ')
if len(A) > 1:
print A[1]
+0
谢谢大家! – user2086043 2013-02-21 03:27:52
答
A[1]
将犯罪嫌疑人如果在文件的最后一行是\n
。您希望重新考虑您将信息拉回的方式。
答
错误是告诉你line
拆分的内容A
没有第二项,这意味着它没有任何剩余的内容可以解析,而且你在文件的末尾。
看起来你的一条线没有任何空格。 – mgilson 2013-02-21 02:37:25