在Python中,当Iam遍历文件目录时,如何在根目录下输入文本文件并用xxxxx替换与re匹配的字符串

问题描述 投票:0回答:1

我想遍历包含许多文件的目录,并在每个文本文件内附加字符串。我可以使用re.findall方法捕获字符串。我想在每个文本文件中用xxxx替换该字符串。

import os
import pandas as pd
import numpy as np
import re

start = os.getcwd()
for (root,dirs,files) in os.walk(start):
    if files.endswith('.txt'):
        keepfile = files
        with open(keepfiles,'a') as newFile:
            content = newFile.read()
            text1 = re.findall('name',content)
            text2 = re.findall(‘_serial..\w+’,content)
            ReplaceWith1= content .replace(text1,'xxxxxx')
            ReplaceWith2= text2.replace(‘text2,’ttttt’)

我想遍历包含许多文件的目录,并在每个文本文件内附加字符串。我可以使用re.findall方法捕获字符串。我想用每个文本中的xxxx替换该字符串...

python string pandas replace os.walk
1个回答
0
投票

您可以执行此操作,但是我会在您已复制的目录中对其进行测试,以确保它符合您的要求,我认为这样做是:

© www.soinside.com 2019 - 2024. All rights reserved.