[英]python read each line in a text file and put anything between START and END in a new file
I have a text file i've imported which has no blank lines and looks like this... Each of these things are on a separate line. 我有一个导入的文本文件,其中没有空行,看起来像这样……这些东西都放在单独的行上。
--START--some data
one line
two line
three line
--END--
four
five
--START-- some data
six
seven
eight
--END--
nine
ten
eleven
--START-- some data
What I want 我想要的是
I have already written code to open the file and loop through each line and find the ones which contain start. 我已经编写了代码来打开文件并循环浏览每一行,并找到包含开始的内容。
import codecs
file = codecs.open('data.txt', encoding='utf-8').read()
for line in file:
if '--START--' in line:
#found the start line (keep all lines until you find END)
What I don't know how to do is create the logic in python where each line that either begins with START or is after that (until but not including the END line) goes into a new text file. 我不知道该怎么做,是在python中创建逻辑,其中每行以START开头或之后的行(直到但不包括END行)都进入一个新的文本文件。
So I would end up with NewFile.txt which contained only: 因此,我最终得到的NewFile.txt仅包含:
--START--some data
one line
two line
three line
--START-- some data
six
seven
eight
--START-- some data
you mean something like 你的意思是
file_contents = open('data.txt',"rb").read()
with open("newfile.txt","wb") as f:
f.write("--START--".join(p.split("--END--")[0] for p in file_contents.split("--START--")))
What about this? 那这个呢?
import codecs file = codecs.open('data.txt', encoding='utf-8').read() startblock = 0 for line in file: if '--END--' in line: startblock = 0 elif '--START--' in line or startblock: # Write to file startblock = 1
from itertools import takewhile
with open("in.txt") as f:
final = []
for line in f:
if line.startswith("--START--"):
final += [line] + list(takewhile(lambda x: not x.startswith("--END--"),f))
print(final)
['--START--some data\n', 'one line\n', 'two line\n', 'three line\n', '--START-- some data\n', 'six \n', 'seven\n', 'eight\n', '--START-- some data']
To write the new data: 要写入新数据:
from itertools import takewhile
with open("in.txt") as f,open("out.txt","w") as f1:
for line in f:
if line.startswith("--START--"):
f1.write(line + "".join(list(takewhile(lambda x: not x.startswith("--END--"),f))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.