2013-02-21 145 views
0

我有一個問題,我需要大家的幫助。 我讀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 

請幫幫我! 謝謝!!!

+0

看起來你的一條線沒有任何空格。 – mgilson 2013-02-21 02:37:25

回答

0

您的一行(可能是您的最後一行)不符合您的期望格式。爲此在你內心的循環:

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

謝謝大家! – user2086043 2013-02-21 03:27:52

0

A[1]將犯罪嫌疑人如果在文件的最後一行是\n。您希望重新考慮您將信息拉回的方式。

0

錯誤是告訴你line拆分的內容A沒有第二項,這意味着它沒有任何剩餘的內容可以解析,而且你在文件的末尾。

相關問題