我正在尝试使用print函数来打印re.match的结果,但它作为print的无效语法返回
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
最后有一个缺失的括号
word = re.match("(0\w+)\W(0\w+)",lower()
它应该是
word = re.match("(0\w+)\W(0\w+)",lower())
改变这个:
word = re.match("(0\w+)\W(0\w+)",lower()
print(word)
成:
word = re.match("(0\w+)\W(0\w+)",lower())
print word
word = re.match("(0\w+)\W(0\w+)",lower()
这是错误的,你忘记在引号后添加右括号并使用.lower()
而不是,lower
它应该是这样的
word = re.match("(0\w+)\W(0\w+)").lower()