我有一个输入文本文件,我正在阅读并将所有内容存储在列表中。之后,我根据列表中的特定文本出现拆分列表。
这是功能。
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"
此工作正常并保存ctrlList1
,直到文本megamon> mfc
出现在主列表中。但是我想在MegaMon> mfc
mfcList1
之后保存这些行。我无法做到这一点。
我试过了:
if not re.search("MegaMon> mfc", str):
print "splitting\n"
continue
else:
mfcList1.append(str)
但这似乎不起作用。我需要将文本文件保存在两个不同的列表中。任何帮助将不胜感激。
答案 0 :(得分: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:]中的索引(写出我的头脑),但通常应该有效。
基本上,这使用枚举来获取“for”循环中当前元素的索引,并且当达到分割点时,将“f”列表的其余部分分配给“mfcList1”列表。
答案 1 :(得分: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)
答案 2 :(得分: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"
答案 3 :(得分:0)
我想我最终采用了这个解决方案并且有效:
def readFile1(file1):
f = file1.readlines()
ctrlList1 = []
mfcList1 = []
mfc = False
for line in f:
if re.search("MegaMon> mfc", line):
mfc = True
if re.findall('\n', line):
v = re.sub('\n', '', line)
if mfc:
mfcList1.append(v)
else:
ctrlList1.append(v)
感谢其他答案。我会尝试一下。
答案 4 :(得分:0)
import re
def split_file(filename, boundary='MegaMon> mfc'):
"""Takes a filename and a delimiter, and returns two lists:
the lines before the delimiter and the lines after it.
"""
pre, post = [], [] # before boundary, after boundary
passed_boundary = False
with open(filename) as f:
for line in f:
line = line.rstrip('\n')
if not passed_boundary and re.search(boundary, line):
passed_boundary = True
if passed_boundary:
post.append(line)
else:
pre.append(line)
return pre, post
这与其他答案基本相似,但它包含在一个函数中。 它具有以下不错的属性: