首先,我假设你没有正确地重复你的字符串(比如“嗨,这是一个示例行。”!=“嗨,这是编辑的行。”)错误,不是故意的(我不能弄清楚)。
我将累积文件命名为与目标目录中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)