使用Python替换多个文件中的多个字符串

2024-04-25 17:31:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件列表和字符串列表,需要在yaml文件中进行替换。我想编写一个函数来接受这个yaml文件并执行search和replace方法。这是我到目前为止得到的

2个文本文件和yaml文件

txt\u 1.txt文件

aB123.Abc
AB345.aBC
ab123.ABC
Ab345.abc

txt\u 2.txt文件

ab123.Abc
AB345.ABC
current_date

yaml\ U文件-cf_硕士.yml你知道吗

input_files:
    - txt_1.txt
    - txt_2.txt
replacement_strings:
    string1:
        from: AB123.ABC
        to: XY000.XYZ
    string2:
        from: AB345.ABC
        to:   XY001.ZYX
    string3:
        from: current_date
        to: '2018-04-07'

其目的是替换所有字符串(从值到值),忽略大小写(不区分大小写)

import yaml
import re

with open('cf_master.yml') as f:
        dataMap = yaml.safe_load(f)

def string_replacer(dataMap):
    for files in dataMap['input_files']:
            with open(dataMap['input_files']) as f:
                input_h = f.read()
    for string in dataMap['replacement_strings']:
            output_h = input_h.replace(
                                      dataMap['replacement_strings'][string]['from'],
                                      dataMap['replacement_strings'][string]['to']
                                      )
    with open(output_dataMap[input_files],"w") as f:
                f.write(output_h)
    return output_dataMap[input_files]

string_replacer(dataMap)

我不明白如何更正此代码。生成的输入文件、yaml文件和新文件都在同一文件夹中


Tags: 文件tofromtxtyamlinputoutputstring
1条回答
网友
1楼 · 发布于 2024-04-25 17:31:24

您可以简化yaml文件。替换字符串不需要索引

input_files:
    - txt_1.txt
    - txt_2.txt
replacement_strings:
    - from: AB123.ABC
      to: XY000.XYZ
    - from: AB345.ABC
      to:   XY001.ZYX
    - from: current_date
      to: '2018-04-07'

至于替换,您可能希望分两次进行替换,首先用临时标记替换,然后返回并用实际替换替换标记。这会阻止替换项相互作用。例如,用'b'替换所有'a',用'c'替换所有'b'。如果没有中间标记步骤,第二个替换将替换所有原始的'b',但也替换被替换的'a'中的所有'b'

import yaml
import re

with open('cf_master.yml') as f:
    data = yaml.safe_load(f)


for filepath in data['input_files']:
    with open(filepath, 'r') as f:
        txt = f.read()

    marker_d = dict()
    for i, d in enumerate(data['replacement_strings']):
        marker = '__$TEMP{}$__'.format(i)
        marker_d[marker] = d['to']
        txt = re.sub(re.escape(d['from']), marker, txt, flags=re.I)

    for marker, s in marker_d.items():
        txt = re.sub(re.escape(marker), s, txt)

    # Save file somewhere?

相关问题 更多 >

    热门问题