赞
踩
这里有一个类似我最近做的修改过的例子,基本上是通过你的文本逐行复制。核心逻辑的基础是附加到当前文件名,在找到新的部分后重置。将使用下一节的第一行作为文件名。在#!/usr/bin/env python
import re
data = """
Channel 9 (1 item)
A woman selling her caravan near Bendigo has been left $1,100 out hosted by
Peter Hitchener A woman selling her caravan near Bendigo has been left $1,100
out of pocket after an elderly couple made the purchase with counterfeit money.
The wildlife worker tried to use the notes to pay for a house deposit, but an
agent noticed the notes were missing the Coat of Arms on one side.
Brief: Radio & TV Demographics: 153,000 (male 16+) • 177,000 (female 16+)
Southern Cross Victoria Bendigo (1 item)
Heathcote Police are warning the residents to be on the lookout a hosted by Jo
Hall Heathcote Police are warning the residents to be on the lookout after a
large dash of fake $50 note was discovered. Victim Marianne Thomas was given
counterfeit notes from a caravan. The Heathcote resident tried to pay the house
deposit and that's when the counterfeit notes were spotted. Thomas says the
caravan is in town for the Spanish Festival.
Brief: Radio & TV Demographics: 4,000 (male 16+) • 3,000 (female 16+)
"""
current_file = None
for line in data.split('\n'):
# Set initial filename
if current_file == None and line != '':
current_file = line + '.txt'
# This is to handle the blank line after Brief
if current_file == None:
continue
text_file = open(current_file, "a")
text_file.write(line + "\n")
text_file.close()
# Reset filename if we have finished this section
# which is idenfitied by:
# starts with Brief - ^Brief
# contains some random amount of text - .*
# ends with ) - )$
if re.match(r'^Brief:.*\)$', line) is not None:
current_file = None
这将输出以下文件
^{pr2}$
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。