Python 3.x re.sub正在用空格替换我文件中的文本。不知道为什么
最近,我尝试了一些修改,看看我的正则表达式字符串是否匹配。这场比赛没有运气,但我不确定这一切是否正确。顶层代码是最新的Python 3.x re.sub正在用空格替换我文件中的文本。不知道为什么,python-3.x,Python 3.x,最近,我尝试了一些修改,看看我的正则表达式字符串是否匹配。这场比赛没有运气,但我不确定这一切是否正确。顶层代码是最新的 import os import re progNumber = 1 text_to_replace = re.compile("^BOWL_PROG6_14G$") replace_string = 'Bowl_Prog1' def sub_ftext(file_path='W:\\BOWL PROGRAMS 14G'): old_text = text_to_
import os
import re
progNumber = 1
text_to_replace = re.compile("^BOWL_PROG6_14G$")
replace_string = 'Bowl_Prog1'
def sub_ftext(file_path='W:\\BOWL PROGRAMS 14G'):
old_text = text_to_replace
count = progNumber
for d_name, dirs, files in os.walk(file_path):
for f_name in files:
f_path = os.path.join(d_name, f_name)
if f_name.endswith('.ls'):
with open(f_path) as txt:
s = txt.read()
for line in txt:
if text_to_replace.search(line):
# Substitute the contents
new_text = replace_string + str(count)
s = re.sub(old_text, new_text, s)
# Write it back into the file
with open(f_path, "w") as txt:
txt.write(s)
count += 1
print(s)
print(old_text)
# for text in text_to_replace:
# print(text)
# dirnames = set(os.path.join(d_name, d) for d in dirs)
# # Traverse subfolders
# if dirnames.startwith('BOWL-'):
# for subs in dirnames:
sub_ftext()
`程序会找到我要查找的字符串,但在每个文件中都不会用任何内容替换它
import os
import re
os.chdir('W:\\BOWL PROGRAMS 14G')
progNumber = 1
text_to_replace = re.compile("^BOWL 14G$")
replacement = ("Bowl_Prog_14G" + str(progNumber))
for d_name, dirs, files in os.walk("."):
for f_name in files:
f_path = os.path.join(d_name, f_name)
if f_name.endswith('.ls'):
with open(f_path) as txt:
for line in txt:
s = txt.read()
#s = s.replace(text_to_replace, replacement)
with open(f_path, "w") as txt:
txt.write(s)
print(re.sub(text_to_replace, replacement, line))
progNumber += 1
只是对现有代码进行一些小的修改。您实际上不需要逐行替换内容,因此我删除了该行
import os
import re
progNumber = 1
text_to_replace = re.compile("^BOWL_14G$")
def sub_ftext(file_path, text_to_replace, progNumber):
for d_name, dirs, files in os.walk(file_path):
for f_name in files:
replacement = ("Bowl_Prog_14G" + str(progNumber))
f_path = os.path.join(d_name, f_name)
if f_name.endswith('.ls'):
with open(f_path) as txt:
sl = txt.read()
# Substitute the contents
sl = re.sub(text_to_replace, replacement, sl)
# Write it back into the file
with open(f_path, "w") as txt:
txt.write(sl)
progNumber += 1
# Traverse subfolders
dirnames = set(os.path.join(d_name, d) for d in dirs)
if dirnames:
for subs in dirnames:
sub_ftext(subs, text_to_replace, progNumber)
if __name__ == '__main__':
sub_ftext(r'C:\Documents\TestFolder', text_to_replace, progNumber)
编辑:如果还需要遍历子文件夹,一种方法是将上面的行包装成一个函数,然后可以递归调用该函数。也可能有其他方法可以做到这一点只是对现有代码进行一些小修改。您实际上不需要逐行替换内容,因此我删除了该行
import os
import re
progNumber = 1
text_to_replace = re.compile("^BOWL_14G$")
def sub_ftext(file_path, text_to_replace, progNumber):
for d_name, dirs, files in os.walk(file_path):
for f_name in files:
replacement = ("Bowl_Prog_14G" + str(progNumber))
f_path = os.path.join(d_name, f_name)
if f_name.endswith('.ls'):
with open(f_path) as txt:
sl = txt.read()
# Substitute the contents
sl = re.sub(text_to_replace, replacement, sl)
# Write it back into the file
with open(f_path, "w") as txt:
txt.write(sl)
progNumber += 1
# Traverse subfolders
dirnames = set(os.path.join(d_name, d) for d in dirs)
if dirnames:
for subs in dirnames:
sub_ftext(subs, text_to_replace, progNumber)
if __name__ == '__main__':
sub_ftext(r'C:\Documents\TestFolder', text_to_replace, progNumber)
import os
import re
def sub_text(file_path):
os.chdir(file_path)
prog_number = 1
text_to_replace = re.compile("BOWL_PROG[0-9]*_14G")
for d_name, dirs, files in os.walk("."):
for f_name in files:
replacement=("Bowl_Prog" + str(prog_number))
f_path = os.path.join(d_name, f_name)
if f_name.endswith('.ls'):
with open(f_path) as line:
ls = line.read()
with open(f_path, "w") as line:
ls = re.sub(text_to_replace, replacement, ls)
line.write(ls)
print(line)
prog_number += 1
print(prog_number)
print(replacement)
if __name__ == '__main__':
sub_text(r"W:\\BOWL PROGRAMS 14G")
编辑:如果还需要遍历子文件夹,一种方法是将上面的行包装成一个函数,然后可以递归调用该函数。可能还有其他的方法可以做到这一点我尝试过用()和不用()来编写替换变量。是否要替换内容并将其写回文件?现在
print(re.sub(text\u to\u replace,replacement,line))
只打印替换的内容,但您仍在使用txt.write将原始内容写回文件
是,我打算替换内容并将新内容写回文件。我已尝试使用()和不使用()写入替换变量。是否要替换内容并将其写回文件?现在print(re.sub(text\u to\u replace,replacement,line))
只打印替换的内容,但您仍然使用txt.write将原始内容写回文件。
是的,我打算替换内容并将新内容写回文件。这让我有点困惑。我正在尝试修改的.ls文件表示它已在记事本中修改,但没有任何更改。实际上它是一个根目录。在该根目录中有多个子目录,每个子目录中又有两个子目录,其中一个子目录文件夹包含我需要访问的.ls文件。@robots101更新了我的答案,允许遍历子目录。如果决定使用thisSo进行更新,则必须对代码进行一次小的重构,以调用函数并向其提供要处理的目录的路径。我尝试过你发布的方法,将代码包装到函数中,但只要底部还在,它似乎就不起作用。然而,当我把它注释掉,只调用函数时,它会部分工作,但会删除整个程序。我不记得我在C#中做过这样的事情时有这么复杂。但这是一个学习曲线,希望我能很快找到答案。不过,我非常感谢你给我的帮助。你给了我一些急需的洞察力。你认为我的问题可能在我的reg表达式中吗?也有一次它读取了整个文本文件,但没有改变任何东西。几乎就像regex没有击中一根火柴一样。你知道我如何测试正则表达式字符串吗。看看能不能用mabey这让我有点困惑。我正在尝试修改的.ls文件表示它已在记事本中修改,但没有任何更改。实际上它是一个根目录。在该根目录中有多个子目录,每个子目录中又有两个子目录,其中一个子目录文件夹包含我需要访问的.ls文件。@robots101更新了我的答案,允许遍历子目录。如果决定使用thisSo进行更新,则必须对代码进行一次小的重构,以调用函数并向其提供要处理的目录的路径。我尝试过你发布的方法,将代码包装到函数中,但只要底部还在,它似乎就不起作用。然而,当我把它注释掉,只调用函数时,它会部分工作,但会删除整个程序。我不记得我在C#中做过这样的事情时有这么复杂。但这是一个学习曲线,希望我能很快找到答案。不过,我非常感谢你给我的帮助。你给了我一些急需的洞察力。你认为我的问题可能在我的reg表达式中吗?也有一次它读取了整个文本文件,但没有改变任何东西。几乎就像regex没有击中一根火柴一样。你知道我如何测试正则表达式字符串吗。看看能不能用,梅比。
import os
import re
def sub_text(file_path):
os.chdir(file_path)
prog_number = 1
text_to_replace = re.compile("BOWL_PROG[0-9]*_14G")
for d_name, dirs, files in os.walk("."):
for f_name in files:
replacement=("Bowl_Prog" + str(prog_number))
f_path = os.path.join(d_name, f_name)
if f_name.endswith('.ls'):
with open(f_path) as line:
ls = line.read()
with open(f_path, "w") as line:
ls = re.sub(text_to_replace, replacement, ls)
line.write(ls)
print(line)
prog_number += 1
print(prog_number)
print(replacement)
if __name__ == '__main__':
sub_text(r"W:\\BOWL PROGRAMS 14G")