簡體   English   中英

python讀取文本文件中的每一行,並將START和END之間的任何內容放入新文件中

[英]python read each line in a text file and put anything between START and END in a new file

我有一個導入的文本文件,其中沒有空行,看起來像這樣……這些東西都放在單獨的行上。

--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

我想要的是

我已經編寫了代碼來打開文件並循環瀏覽每一行,並找到包含開始的內容。

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)

我不知道該怎么做,是在python中創建邏輯,其中每行以START開頭或之后的行(直到但不包括END行)都進入一個新的文本文件。

因此,我最終得到的NewFile.txt僅包含:

--START--some data
one line
two line
three line
--START-- some data
six 
seven
eight
--START-- some data

你的意思是

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--")))

那這個呢?

 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']

要寫入新數據:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM