当前位置:   article > 正文

python 文本分块_单独的文本块python

python 文本块

这里有一个类似我最近做的修改过的例子,基本上是通过你的文本逐行复制。核心逻辑的基础是附加到当前文件名,在找到新的部分后重置。将使用下一节的第一行作为文件名。在#!/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}$

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/924738
推荐阅读
相关标签
  

闽ICP备14008679号