擅长:python、mysql、java
<p>如果文件适合内存,则可以使用正则表达式拆分文件,然后对匹配项进行迭代:</p>
<pre><code>with open('models.txt') as handle:
models = re.findall("Model.*?END", handle.read(), re.MULTILINE|re.DOTALL)
for i, model in enumerate(models):
with open('model_%s.txt' % i) as output:
output.write(model)
</code></pre>