import re
with open('input.txt', 'r') as f:
data = f.read()
found = re.findall(r'\n*(A.*?\n\$\$)\n*', data, re.M | re.S)
[open(str(i)+'.txt', 'w').write(found[i-1]) for i in range(1, len(found)+1)]
前3次出现的 简约方法:
import re
found = re.findall(r'\n*(A.*?\n\$\$)\n*', open('input.txt', 'r').read(), re.M | re.S)
[open(str(found.index(f)+1)+'.txt', 'w').write(f) for f in found[:3]]
一些解释:
found = re.findall(r'\n*(A.*?\n\$\$)\n*', data, re.M | re.S)
将查找与指定RegEx匹配的所有匹配项,并将它们放入 ,称为found
[open(str(found.index(f)+1)+'.txt', 'w').write(f) for f in found]
遍历(属于列表)所有元素(使用列表推导),found
并为每个元素创建文本文件(称为“ index of the element + 1
.txt”),并将该元素(出现)写入该文件。
没有RegEx的另一个版本:
blocks_to_read = 3
blk_begin = 'A'
blk_end = '$$'
with open('35916503.txt', 'r') as f:
fn = 1
data = []
write_block = False
for line in f:
if fn > blocks_to_read:
break
line = line.strip()
if line == blk_begin:
write_block = True
if write_block:
data.append(line)
if line == blk_end:
write_block = False
with open(str(fn) + '.txt', 'w') as fout:
fout.write('\n'.join(data))
data = []
fn += 1
PS我个人不喜欢这个版本,我会使用RegEx