Python打印功能返回语法错误

时间:2019-02-13 09:37:03

标签: python automation rhel rhel6

我正在尝试使用打印功能来打印重新匹配的结果,但它作为打印的无效语法而返回

python版本是2.6.6

import re

def word_replace(text, replace_dict):
        rc = re.compile(r"[a-zA-Z]\w*")

def word_replace(text, replace_dict):
        word = re.match("(0\w+)\W(0\w+)",lower()
        print(word)
        return replace_dict.get(word, word)

        return rc.sub(translate, text)

old_text = open('1549963864952.xml').read()

replace_dict = {
"value" : 'new_value',
"value1" : 'new_value1',
"value2" : 'new_value2',
"value3" : 'new_value3'

}                                       # {"Word to find" : 'Word to replace'}

output = word_replace(old_text, replace_dict)
f = open("1549963864952.xml", 'w') # File you want to write to
f.write(output)                                    # Write to that file
print(output)                                      # Check that it wrote

应该返回并打印 word = re.match("(0\w+)\W(0\w+)",lower() 的结果,但我却收到以下错误:

File "location.py", line 8
print(word)
    ^
SyntaxError: invalid syntax

3 个答案:

答案 0 :(得分:2)

末尾缺少括号
word = re.match("(0\w+)\W(0\w+)",lower()

应该是

    word = re.match("(0\w+)\W(0\w+)",lower())

答案 1 :(得分:1)

更改此内容:

 word = re.match("(0\w+)\W(0\w+)",lower()
 print(word)

进入:

 word = re.match("(0\w+)\W(0\w+)",lower())
 print word

答案 2 :(得分:0)

word = re.match("(0\w+)\W(0\w+)",lower()

这是错误的,您忘记在引号后添加右括号,而使用.lower()而不是,lower

应该是这样

word = re.match("(0\w+)\W(0\w+)").lower()
相关问题