簡體   English   中英

如何再次合並和拆分數千個文本文件?

[英]How to merge and split again thousands of text files?

我有數千個 .txt 文件。 這些文本文件包括一個字符串。 (每個文件都有不同的字符串。)

我想編輯這些字符串,但我不想手動逐個打開每個文件進行編輯。 所以我想將所有這些文件合並到一個 .txt 文件中,在我完成編輯后,我想用合並之前它們擁有的相同文件名再次分離/拆分它們。

例如;

我有這些文本文件。

lorem.txt(嗨,這是一個示例行。)

ipsum.txt(嗨,這是另一行。)

merol123.txt(嗨,只是另一行。)

*

合並的.txt >>> 編輯並准備再次拆分。 >> 結果需要這樣;

*

lorem.txt(嗨,這是編輯過的行。)

ipsum.txt(另一個編輯過的行。)

merol123.txt(另一行編輯。編號 4847887)

注意:括號內的句子代表txt文件內的字符串。

是否可以? 我在等你的幫助,謝謝!

首先,我假設你沒有正確地重復你的字符串(比如“嗨,這是一個示例行。”!=“嗨,這是編輯過的行。”),不是故意的(我不能弄清楚)。

我將累積文件common.doc命名為與目標目錄中的其他.txt文件不同。 此外,此示例代碼意味着所有文件都在同一目錄中。

# merging.py
import os
import glob

with open("common.doc", "w") as common:
    for txt in glob.glob("./*.txt"):
        with open(txt, "r") as f:
            content = f.read()
        common.write("{} ({})\n".format(os.path.basename(txt), content))

common.doc編輯之后:

# splitting.py
with open("common.doc", "r") as common:
    for line in common:
        name = line[:line.find(" (")]
        text = line[line.find(" (")+2:line.rfind(")")]
        with open(name, "w") as f:
            f.write(text)

以及多行文本的解決方案(合並保留在內容寫入時刪除.strip() ),不適用於數十萬個文件...

# splitting2.py
with open("common.doc", "r") as common:
    everything = common.read()
elements = everything.split(")")
for elem in elements:
    name = elem[:elem.find(" (")].strip()
    text = elem[elem.find(" (")+2:]
    if name:
        with open(name, "w") as f:
            f.write(text)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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