Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用字典中的字符串替换python正则表达式_Python_Regex_String - Fatal编程技术网

用字典中的字符串替换python正则表达式

用字典中的字符串替换python正则表达式,python,regex,string,Python,Regex,String,我正在尝试从模板文件创建一个文件。 模板有一些元素需要根据用户输入或配置文件动态设置。 该模板包含下面代码中的正则表达式实例。 我想做的只是用字典中的已知值替换正则表达式中包含的单词(\w)。 下面是我的代码: def write_cmake_file(self): # pass with open (os.path.join(os.getcwd(), 'templates', self.template_name)) as f: lines = f.readli

我正在尝试从模板文件创建一个文件。 模板有一些元素需要根据用户输入或配置文件动态设置。 该模板包含下面代码中的正则表达式实例。 我想做的只是用字典中的已知值替换正则表达式中包含的单词
(\w)
。 下面是我的代码:

def write_cmake_file(self):
    # pass
    with open (os.path.join(os.getcwd(), 'templates', self.template_name)) as f:
        lines = f.readlines()

    def replace_key_vals(match):
        for key, value in template_keys.iteritems():
            if key in match.string():
                return value

    regex = re.compile(r">>>>>{(\w+)}")
    for line in lines:
        line = re.sub(regex, replace_key_vals, line)

    with open(os.path.join(self.project_root, 'CMakeLists.txt'), 'w') as cmake_file:
        cmake_file.write(lines)
python解释器抱怨类型错误:“str”对象不可调用。 我想知道此代码不起作用的原因以及修复方法。

将代码更改为:

regex = re.compile(r">>>>>{(\w+)}")
for line in lines:
    line = regex.sub(replace_key_vals, line)
    #      ---^---
您正在编译正则表达式,之后试图将其用作字符串,但这不起作用。

将代码更改为:

regex = re.compile(r">>>>>{(\w+)}")
for line in lines:
    line = regex.sub(replace_key_vals, line)
    #      ---^---

您当时正在编译正则表达式,后来试图将其用作字符串,但这行不通。

以下代码修复了我的问题:

def write_cmake_file(self):
    # pass
    with open (os.path.join(os.getcwd(), 'templates', self.template_name)) as f:
        lines = f.readlines()

    def replace_key_vals(match):
        print match.string
        for key, value in template_keys.iteritems():
            if key in match.string:
                return value

    regex = re.compile(r">>>>>{(\w+)}")
    # for line in lines:
        # line = regex.sub(replace_key_vals, line)
    lines = [regex.sub(replace_key_vals, line) for line in lines]

    with open(os.path.join(self.project_root, 'CMakeLists.txt'), 'w') as cmake_file:
        cmake_file.writelines(lines)

以下代码修复了我的问题:

def write_cmake_file(self):
    # pass
    with open (os.path.join(os.getcwd(), 'templates', self.template_name)) as f:
        lines = f.readlines()

    def replace_key_vals(match):
        print match.string
        for key, value in template_keys.iteritems():
            if key in match.string:
                return value

    regex = re.compile(r">>>>>{(\w+)}")
    # for line in lines:
        # line = regex.sub(replace_key_vals, line)
    lines = [regex.sub(replace_key_vals, line) for line in lines]

    with open(os.path.join(self.project_root, 'CMakeLists.txt'), 'w') as cmake_file:
        cmake_file.writelines(lines)

@兰蔻:你有一些输入字符串吗?我解决了这个问题;这实际上是因为我调用了
match.string()
。它是类成员,不是函数。然而,新的问题是,虽然代码不会导致运行时错误,但在从template@Lancophone:你有一些输入字符串吗?我解决了这个问题;这实际上是因为我调用了
match.string()
。它是类成员,不是函数。然而,新的问题是,虽然代码不会导致运行时错误,但在从模板创建输出文件时,它实际上不会替换任何内容。如果不更改
列表,
变量更改不会修改
。是的!谢谢你发现了!您不会更改
列表,
变量更改不会修改
。是!谢谢你发现了!