您可以使用以下方法,利用Python的CSV库将行拆分为适当的列。这样就可以更容易地支持不同的分隔符或额外的引号(如果需要)。在import StringIO, csv
with open('file.txt', 'r') as f_input:
begin = False
rows = []
for line in f_input:
if line.startswith("end of file"):
break
elif begin:
rows.append(next(csv.reader(StringIO.StringIO(line))))
elif line.startswith("begin of file"):
begin = True
print rows
因此,对于以下类型的输入:
^{pr2}$
它将创造:[['1', '2', '3', '4', '5', '6', '7'], ['8', '9', '10'], ['11', '12', '13', '14', '15']]