我得到" INDEXERROR:列表索引超出范围"问题。
我写的类似代码不会出现。
我用------- sign -------。
表示错误行。 。 。 。 。 。
#import json
result = {}
#declare variables
count = -1
category = None
paragraph = None
#list of files
file_lists = ["sc.txt" ]
for file in file_lists:
#read line
for line in open(file,'r').readlines():
pieces = line.split('\t')
#if category is empty assign 'category' from line 1
if (category == None):
category = pieces[0]
#if starts with 'Question' skip
elif (pieces[0].startswith('Question')):
continue
#if------ ERROR OCCURS RIGHT BELOW THIS LINE !!!!!------------
elif ((pieces[0] != '') and (pieces[1] == '')):
paragraph = pieces[0]
#add incorrect to the dict
elif ((pieces[0] != '') and (pieces[1] != '')):
count += 1
result[count] = {'Category': category,
'Question': paragraph,
'Given_sen': pieces[0],
'Incorrect':[pieces[1]],
'Correct': pieces[2],
'Rule': pieces[3]
}
elif (pieces[0].startswith('')):
result[count]['Incorrect'].append(pieces[1])
答案 0 :(得分:0)
您可以在下面的代码中添加try以及跟踪哪行代码
using MvvmCross.iOS.Views.Presenters;
或者,如果您的所有行总是被拆分为相同长度的元素。您可以检查for line in open(file,'r').readlines():
try:
pieces = line.split('\t')
...
except IndexError as ie:
print ie
如果长度不等于预期长度值,请使用len(pieces)
忽略其余代码。