我想遍历包含许多文件的目录,并在每个文本文件内附加字符串。我可以使用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替换该字符串...
您可以执行此操作,但是我会在您已复制的目录中对其进行测试,以确保它符合您的要求,我认为这样做是: