【发布时间】:2023-01-15 15:23:20
【问题描述】:
我有一个文本文件,看起来像下面的虚拟文件
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and
some random characters and then start of my data
some characters in between
some characters in between
some characters in between
some characters in between
some characters in between
some characters in between
end of my data
scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised
in the 1960s with the release of Letraset
when an unknown printer took a galley of type and
some random characters and then start of my data
some characters in between
some characters in between
some characters in between
some characters in between
some characters in between
some characters in between
end of my data
sheets containing Lorem Ipsum passages,
and more recently with desktop publishing
when an unknown printer took a galley of type and
some random characters and then start of my data
some characters in between
some characters in between
some characters in between
some characters in between
some characters in between
some characters in between
end of my data
software like Aldus PageMaker including
versions of Lorem Ipsum.
我想在“我的数据开始”到“我的数据结束”之间提取数据并将其保存在列表变量中。此数据在文本文件中多次出现。我尝试了下面的代码
import re
import sys
s=[]
with open('mytextfile.txt','r') as file:
mystring = file.read()
myre = re.compile(r"start of my data(.*?)end of my data", re.DOTALL)
parts = myre.findall(mystring)
s.append(parts)
此代码将所有找到的字符串一次保存在列表的第一个索引上。但我需要新索引上的每个单独数据。我怎样才能做到这一点?
【问题讨论】:
-
按换行符拆分数据?
-
是的,换行符从数据开始到数据结束
-
好的,那就去做吧。