用标题分割文本文件
我有一个输入的文本文件,我正在读取这个文件并把所有内容存储到一个列表里。之后,我根据列表中特定文本的出现来拆分这个列表。
这里是这个功能的代码。
import re
def readFile1(file1):
f = file1.read().split('\n')
#print f
ctrlList1 = []
mfcList1 = []
for str in f:
if re.search("MegaMon> mfc",str):
print "splitting\n"
break
else:
ctrlList1.append(str)
print ctrlList1, "\n\n\n"
这个功能运行得很好,它会在主列表中找到文本 megamon> mfc
出现的位置,并把之前的内容保存到 ctrlList1
里。但是我想把 MegaMon> mfc
之后的行保存到 mfcList1
里。可是我做不到这一点。
我尝试过:
if not re.search("MegaMon> mfc", str):
print "splitting\n"
continue
else:
mfcList1.append(str)
但是这似乎没有效果。我需要把文本文件的内容保存到两个不同的列表中。任何帮助都会很感激。
5 个回答
0
其他的解决方案看起来都不错。不过这个方案似乎更优雅一些:
ctrlList1=[]
mfcList1=[]
curlist = ctrlList1 # Initially, append to ctrlList1
for line in file1:
str = line.rstrip("\n") # Remove trailing newlines
if re.search("MegaMon> mfc",str):
print "splitting\n"
curlist = mfcList1 # From now on, append to mfcList1
continue
curlist.append(str)
print ctrlList1, "\n\n\n"
print mfclList1, "\n\n\n"
1
这样怎么样:
for index, str in enumerate(f):
if re.search("MegaMon> mfc",str):
print "splitting\n"
mfcList1=f[ index + 1 : ]
break
else:
ctrlList1.append(str)
你可能需要把[index + 1 : ]里的索引改一下(这是我随便想的),但总体来说应该是可以工作的。
基本上,这段代码使用了enumerate来获取“for”循环中当前元素的索引,当达到分割点时,就把“f”列表剩下的部分赋值给“mfcList1”列表。
0
那怎么样呢
import re
mfcList1, ctrlList1 = [],[]
# read the whole file as a list of lines - its easier
with open(file1, 'r') as f1:
lines = f1.readlines()
# for each line, search for your string.
# If you have found MegaMon append one mfcList1, else append ctrlList1
foundMegaMon = False
for line in lines:
if re.search("MegaMon> mfc",line):
foundMegaMon = True
if foundMegaMon:
mfcList1.append(line)
else:
ctrlList1.append(line)