[英]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))
这似乎没有返回任何内容,我尝试了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')
解释:前两个语句读取文件,删除所有换行符并将结果放入单个字符串“数据”中。 最后一条语句在“特殊字符”'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
如果您想“定位以 '03' 开头的所有位置实例”,您应该检查x.startswith("03")
而不是x == "03"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.