如何通过将文件中的数据替换为相同字符串来合并分隔行



大多数人都认为它是重复的其实不是,所以我要做的是假设有一个像下面这样的Master字符串其中包含了几个文件然后我们需要打开这些文件并检查其中是否包含其他文件,如果有的话我们需要将其复制到我们获取特定文本的行中

主字符串:

欢迎
你好吗?
file.txt
一切好了
signature.txt
由于

file.txt


ABCD EFGH
tele.txt

tele.txt

IJKL

signature.txt

SAK

输出:

欢迎
你好吗?
ABCD
EFGH
IJKL
一切都好
SAK
由于

for msplitin [stext.split('n')]:
            for num, items in enumerate(stext,1):
                if items.strip().startswith("here is") and items.strip().endswith(".txt"):
                       gmsf = open(os.path.join(os.getcwd()+"txt", items[8:]), "r")
                        gmsfstr = gmsf.read()
                        newline = items.replace(items, gmsfstr)

如何以相同的字符串格式连接这些替换项。

还有,关于如何重复相同的函数,直到没有".txt"的想法。因此,一旦连接完成,可能会在"。txt"中出现其他"。txt"。

提前感谢您的帮助

适用于任何级别的文件名嵌套的递归方法:

from os import linesep
def get_text_from_file(file_path):
    with open(file_path) as f:
        text = f.read()
        return SAK_replace(text)
def SAK_replace(s):
    lines = s.splitlines()
    for index, l in enumerate(lines):
        if l.endswith('.txt'):
            lines[index] = get_text_from_file(l)
    return linesep.join(lines)

您可以尝试:

s = """Welcome
How are you
here is file.txt
everything alright
here is signature.txt
Thanks"""
data = s.split("n")
match = ['.txt']
all_matches = [s for s in data if any(xs in s for xs in match)]
for index, item in enumerate(data):
    if item in all_matches:
        data[index] ="XYZ"
data = "n".join(data)
print data
输出:

Welcome
How are you
XYZ
everything alright
XYZ
Thanks

新增要求:

def file_obj(filename):
        fo = open(filename,"r")
        s = fo.readlines()
        data = s.split("n")
        match = ['.txt']
        all_matches = [s for s in data if any(xs in s for xs in match)]
        for index, item in enumerate(data):
                if item in all_matches:
                        file_obj(item)
                        data[index] ="XYZ"
        data = "n".join(data)
        print data
file_obj("first_filename") 

我们可以创建临时文件对象,并将被替换的行保存在该临时文件对象中,一旦所有行都处理完毕,我们就可以将新内容替换为原始文件。这个临时文件从'with'语句中出来后会被自动删除。

import tempfile
import re
file_pattern = re.compile(ur'(((w+).txt))')
original_content_file_name = 'sample.txt'
"""
sample.txt should have this content.
Welcome
How are you
here is file.txt
everything alright
here is signature.txt
Thanks
"""
replaced_file_str = None

def replace_file_content():
    """
    replace the file content using temporary file object.
    """
    def read_content(file_name):
        # matched file name is read and returned back for replacing.
        content = ""
        with open(file_name) as fileObj:
            content = fileObj.read()
        return content
    # read the file and keep the replaced text in temporary file object(tempfile object will be deleted automatically).
    with open(original_content_file_name, 'r') as file_obj, tempfile.NamedTemporaryFile() as tmp_file:
        for line in file_obj.readlines():
            if line.strip().startswith("here is") and line.strip().endswith(".txt"):
                file_path = re.search(file_pattern, line).group()
                line = read_content(file_path) + 'n'
            tmp_file.write(line)
        tmp_file.seek(0)
        # assign the replaced value to this variable
        replaced_file_str = tmp_file.read()
    # replace with new content to the original file
    with open(original_content_file_name, 'w+') as file_obj:
        file_obj.write(replaced_file_str)
replace_file_content()

最新更新