简体   繁体   English

python读取文本文件中的每一行,并将START和END之间的任何内容放入新文件中

[英]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.

相关问题 从开始在文本文件的每一行末尾添加逗号 - Add comma at end of each line in text file from start 读取文本文件的每一行,然后在python中用空格分隔每一行 - Read each line of a text file and then split each line by spaces in python Python - 读取文本文件的每一行并将每一行传递给变量 - Python - Read each line of text file and pass each line to variable 将数组重写为文本文件,需要将每个部分放在新行上 - Rewriting array to text file, need to put each part on new line Python 3 文本文件 - 我想将用户输入添加到文件 1 中每行文本的末尾,然后将新行复制到文件 2 中 - Python 3 text files - I want to add add user input to the end of each line of text in file 1 then copy the new lines into file 2 如何读取CSV或文本文件的行,循环遍历每行并保存为每行读取的新文件 - How To Read Lines of CSV or Text File, Loop Over Each Line and Save To a New File For Each Line Read 创建数据,然后将数据附加到python文本文件中每行的末尾 - Creating, then appending data on to the end of each line on a text file in python Python:当每个键和值都在新行上时,如何将文本文件读入字典? - Python: How to read a text file into a dictionary when each key and value is on a new line? 在 Python 中的文件末尾添加文本,但不创建新行 - Add text to the end of a file in Python, but without creating a new line Python:读取一个文本文件(每行都是字典结构)到字典 - Python: read a text file (each line is of dictionary structure) to a dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM