根据项目中的值从列表中提取位置

extracting positions from list based on values in items

我对使用 python 比较陌生。我正在尝试采用标准文件格式,并最终根据出现在一行中的某个标识符将其分解成更小的文件。

到目前为止,我已经能够获取文件,打开它进行读写,然后将每一行分解为一个列表项。现在我试图找到以“03”开头的每个列表项位置。从一个“03”列表位置到另一个列表位置的所有内容最终将成为一个单独的文件。我一直在尝试提取列表值包含“03”的列表位置。我试过使用:

for value in acct_locate:
    if value == '03':
        locations.append(acct_locate.index(value))

这似乎是 return 什么都没有,我已经尝试了一些其他版本的 enumerate()index()

目前这是我正在使用的代码:

import re
#need to look for file name
filename = 'examplebai2.txt'

#this list will store all locations where three record shows up
acct_locate = []
locations = []
acct_listing = []

with open(filename, 'r+') as file:
    line = [line.rstrip('\n') for line in file]
    for x in line:
        #locate all instances of locations starting with '03'
        look = re.findall('^03', x)
        acct_locate.append(look)
        #add those instances to a new list
    a = [i for i,x in enumerate(acct_locate) if x == '03']
    for value in a:
        print(value)
        locations.append(acct_locate.index(value))
    for y in line:
        namelist = re.findall('^03, (.*),', y)
        if len(namelist) > 0:
            acct_listing.append(namelist)

运行 上面的代码 return 对我用来收集所有位置的 locations 列表没有任何影响。

这是我要操作的文件的框架。

01, Testfile
02, Grouptest
03, 11111111
16
88
49
03, 22222222,
16
88
49
03, 33333333,
16
88
49
03, 44444444,
16
88
49
98, Grouptestclose
99, Testfileclose

从这个文件开始,我想以四个单独的文件结束,这些文件包含从一个 03 记录到下一个 03 记录。

如果您不需要知道特殊字符的位置,您可以这样做:

with open('examplebai2.txt', 'r') as file:
    data = file.read().replace('\n', ' ')

data = data.split('03')

解释:前两个语句读取文件,删除所有换行符并将结果放入单个字符串"data"。最后一条语句根据 "special character" '03' 的出现拆分字符串,返回一个字符串列表,其中每个元素都是两个 '03' 之间的一部分。

编辑:

鉴于上面的示例数据,您可以尝试遍历文件并将读取的数据放入缓冲区。每次找到“03”时,将缓冲区清空到一个新文件中。示例:

buffer = ""
new_file_counter = 0
with open(filename,'r+') as file:
    ## loop over lines
    for x in file:
        if x.split(',')[0] == '03':
            with open('out_file_{}'.format(new_file_counter)) as out:
                out.write(buffer)
                buffer = ""
                new_file_counter = 0
        buffer += x


如果你想 "locate all instances of locations starting with '03'",你应该检查 x.startswith("03") 而不是 x == "03"