简体   繁体   English

IndexError:列表索引超出范围?

[英]IndexError: list index out of range?

I have a problem, I need the help of everyone. 我有一个问题,我需要大家的帮助。 I read rar file (100mb) and process text file (include in rarfile). 我阅读了rar文件(100mb)并处理了文本文件(包括在rarfile中)。

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]

Result: 结果:

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

Please, help me! 请帮我! Thank you!!! 谢谢!!!

One of your lines (probably your last) is not in the format you expect. 您的一行(可能是最后一行)的格式不符合您的预期。 Do this in your inner for loop: 在您的内部for循环中执行此操作:

A=line.split(' ')
if len(A) > 1:
    print A[1]

A[1] would be suspect if the last line in your file was \\n . 如果文件中的最后一行是\\n则会怀疑A[1] You'd want to rethink the way you're pulling info back. 您想重新考虑获取信息的方式。

错误告诉您, line拆分的内容A没有第二个项目,这意味着它没有任何要解析的内容,并且您位于文件的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM