re.sub将文件中的文本替换为空白。不知道为什么

时间:2019-03-14 00:41:46

标签: 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_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

2 个答案:

答案 0 :(得分:0)

只需对现有代码进行一些小改动即可。您实际上不需要逐行替换内容,因此我已删除了该行。

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)

编辑:如果还需要遍历子文件夹,一种方法是将上面的行包装到一个函数中,然后可以递归调用该函数。可能还有其他方法可以做到

答案 1 :(得分:0)

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")