gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:41:13 25 4
gpt4 key购买 nike

我对使用 python 还比较陌生。我试图采用标准文件格式,并最终根据行上出现的特定标识符将其分解为更小的文件。

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

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

这似乎没有返回任何内容,我尝试了 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)

运行上述代码不会向我用来收集所有位置的 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”中。最后一条语句在出现“特殊字符”“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


关于python - 根据项目中的值从列表中提取位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59449702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com