<p>我有一个很大的文本文件,<br/>
其中每一行都是根据define语法(这是用regex处理的)。你知道吗</p>
<p>我得到以下错误:</p>
<pre><code>remainder = '{} {} '.format(*pieces[-1])
IndexError: list index out of range
</code></pre>
<p>在此代码上:</p>
<pre><code>def open_delimited(filename, args):
with open(filename, args, encoding="UTF-16") as infile:
chunksize = 10000
remainder = ''
for chunk in iter(lambda: infile.read(chunksize), ''):
pieces = re.findall(r"(\d+)\s+(\d+_\d+)\s+(((post)\s+1)|((\d+_\d+_\d+)\s+(comment)\s+2))(.+)(\d{4}-\d{2}-\d{2})\s+(\d{2}:\d{2}:\d{2})(.*)", remainder + chunk, re.IGNORECASE)
for piece in pieces[:-1]:
yield piece
remainder = '{} {} '.format(*pieces[-1])
if remainder:
yield remainder
filename = 'data/AllData_2000001_3000000.txt'
for chunk in open_delimited(filename, 'r'):
for j in range(len(chunk)):
print(chunk[j])
</code></pre>
<p>当我限制迭代次数时,代码运行良好。你知道吗</p>
<pre><code>i = 0
for chunk in open_delimited(filename, 'r'):
if (i <= 1000):
for j in range(len(chunk)):
print(chunk[j])
else:
break
i += 1
</code></pre>
<p>有没有可能<code>pieces</code>是空的?你知道吗</p>
<pre><code>>>> [][-1]
IndexError: list index out of range
</code></pre>
<p>我最好的猜测是<code>re.findall</code>有时找不到任何东西。你知道吗</p>