[英]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.