Python 在特定行之间解析文本文件
因此,如果我有一个类似这样的文本文件,我想创建每个数据块的列表Python 在特定行之间解析文本文件,python,list,parsing,text-files,newline,Python,List,Parsing,Text Files,Newline,因此,如果我有一个类似这样的文本文件,我想创建每个数据块的列表 [Blocktype A] thing thing thing [Blocktype A] thing thing thing thing thing [Blocktype A] thing thing [Blocktype B] thing thing thing 本质上,我希望我的代码能够做到这一点 如果行=='[Blocktype A]',则将下一个X行数(可以变化)附加到“块/节”列表中,直到到达换行。此时,将此“块
[Blocktype A]
thing
thing
thing
[Blocktype A]
thing
thing
thing
thing
thing
[Blocktype A]
thing
thing
[Blocktype B]
thing
thing
thing
本质上,我希望我的代码能够做到这一点
如果行=='[Blocktype A]',则将下一个X行数(可以变化)附加到“块/节”列表中,直到到达换行。此时,将此“块”列表附加到一个整体列表中,清空“块”列表,并对下一个块类型A节执行相同操作,直到到达新行为止。我想对“[Blocktype B]”执行相同操作
最后,我试图得到一个列表,其中包含子列表作为元素。换句话说,[Blocktype a]列表数据的列表和所有[Blocktype B]列表数据的列表
bigListA=[['Blocktype A'、'thing'、'thing'、'thing']、['Blocktype A'、'thing'、'thing'、'thing']等]
bigListB=同上
我不确定如何在这样的特定行之间解析。有什么想法吗?非常感谢
编辑*这是我的代码。问题是,['B']节被添加到了不应该添加的列表中。我觉得我的列表清空步骤已经结束了。我刚刚发现的另一个问题是,当我打印出返回列表的元素时,每个元素都是相同的(只有文件中的第一个块……它只是重复)
像这样:
bigLists = ([z.strip('[').strip(']') for z in y.split('\n') if z]
for y in x.split('\n\n'))
bigListA = [x for x in bigLists if x[0] == 'Blocktype A']
bigListB = [x for x in bigLists if x[0] == 'Blocktype B']
像这样:
bigLists = ([z.strip('[').strip(']') for z in y.split('\n') if z]
for y in x.split('\n\n'))
bigListA = [x for x in bigLists if x[0] == 'Blocktype A']
bigListB = [x for x in bigLists if x[0] == 'Blocktype B']
我喜欢为此使用生成器函数:
import itertools
from pprint import pprint
def stanzas(f):
stanza = []
for line in f:
line = line.strip()
if line.startswith('['):
if stanza:
yield stanza
stanza = []
if line:
stanza += [line]
if stanza:
yield stanza
with open('foo.ini') as input_file:
all_data = stanzas(input_file)
all_data = sorted(all_data, key = lambda x:x[0])
all_data = itertools.groupby(all_data, key = lambda x:x[0])
all_data = {k:list(v) for k,v in all_data}
# All of the data is in a dict in all_data. The dict keys are whatever
# stanza headers in the file there were.
# We can extract out the bits we want using []
bigListA = all_data['[Blocktype A]']
bigListB = all_data['[Blocktype B]']
pprint(bigListA)
pprint(bigListB)
我喜欢为此使用生成器函数:
import itertools
from pprint import pprint
def stanzas(f):
stanza = []
for line in f:
line = line.strip()
if line.startswith('['):
if stanza:
yield stanza
stanza = []
if line:
stanza += [line]
if stanza:
yield stanza
with open('foo.ini') as input_file:
all_data = stanzas(input_file)
all_data = sorted(all_data, key = lambda x:x[0])
all_data = itertools.groupby(all_data, key = lambda x:x[0])
all_data = {k:list(v) for k,v in all_data}
# All of the data is in a dict in all_data. The dict keys are whatever
# stanza headers in the file there were.
# We can extract out the bits we want using []
bigListA = all_data['[Blocktype A]']
bigListB = all_data['[Blocktype B]']
pprint(bigListA)
pprint(bigListB)
输出正是您所需要的
def bigList(list_name,start):
quit_ask = ""
list_name = []
l = []
check = True
started = False
with open("TEXT.txt") as text_file:
for line in text_file:
line = line.strip()
if line.startswith(start) or started == True:
while '' in l: l.remove('')
if line.startswith(start):
quit_ask = line
if check != True:
list_name.append(l)
l = []
l.append(line)
started = True
elif line.startswith('[') and line != quit_ask: break
else: l.append(line); check = False
list_name.append(l)
return list_name
bigListA = []
bigListB = []
bigListA = bigList(bigListA,'[Blocktype A]')
bigListB = bigList(bigListB,'[Blocktype B]')
print bigListA
print bigListB
而且你不会被迫进口任何东西 输出正是您需要的
def bigList(list_name,start):
quit_ask = ""
list_name = []
l = []
check = True
started = False
with open("TEXT.txt") as text_file:
for line in text_file:
line = line.strip()
if line.startswith(start) or started == True:
while '' in l: l.remove('')
if line.startswith(start):
quit_ask = line
if check != True:
list_name.append(l)
l = []
l.append(line)
started = True
elif line.startswith('[') and line != quit_ask: break
else: l.append(line); check = False
list_name.append(l)
return list_name
bigListA = []
bigListB = []
bigListA = bigList(bigListA,'[Blocktype A]')
bigListB = bigList(bigListB,'[Blocktype B]')
print bigListA
print bigListB
而且你不会被迫进口任何东西 你试过什么吗?有代码吗?是的,但我不想发布,因为我需要编辑很多私人数据。我一直在试着把它读成内嵌的行…等等。。。。if elif else阻止说if line.strip()=='[Blocktype A]'…做点什么等等。我的问题是何时清空每个小节的列表。我不确定是否应该在更大的范围内创建列表,然后del myList[:]当新行被添加后重新开始时,如果您想像解析配置文件一样解析配置文件,请使用
ConfigParser
module,使用起来非常简单方便。块类型之间的行数是否会超过您想要的行数?i、 e.6行,您只需要前3行。我想在blocktype之后追加所有行,直到到达换行符。然后将现在填充的列表转储到一个更大的列表中,清空它,并对下一节/区块执行相同的操作。您尝试过什么吗?有代码吗?是的,但我不想发布,因为我需要编辑很多私人数据。我一直在试着把它读成内嵌的行…等等。。。。if elif else阻止说if line.strip()=='[Blocktype A]'…做点什么等等。我的问题是何时清空每个小节的列表。我不确定是否应该在更大的范围内创建列表,然后del myList[:]当新行被添加后重新开始时,如果您想像解析配置文件一样解析配置文件,请使用ConfigParser
module,使用起来非常简单方便。块类型之间的行数是否会超过您想要的行数?i、 e.6行,您只需要前3行。我想在blocktype之后追加所有行,直到到达换行符。然后将现在填充的列表转储到一个更大的列表中,清空它,并对下一个节/块执行相同的操作。我不清楚OP是想将节定义为“以空行结束”还是“以[
开始”,因此我(任意)选择了“以[
开始”。如果需要其他行为,则更改是无关紧要的。我不清楚OP是否要将节定义为“以空行结束”或“以[
开始”,因此我(任意)选择了“以[
开始”。如果需要其他行为,则更改是无关紧要的。