Python打印函数返回语法错误
我正试图使用print函数来打印重新匹配的结果,但它作为无效的print语法返回 python版本是2.6.6Python打印函数返回语法错误,python,automation,rhel,rhel6,Python,Automation,Rhel,Rhel6,我正试图使用print函数来打印重新匹配的结果,但它作为无效的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(wor
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()
print(word)
进入:
本节末尾缺少一个括号
word = re.match("(0\w+)\W(0\w+)",lower()
应该是
word = re.match("(0\w+)\W(0\w+)",lower())
这是错误的,您忘记在引号后添加右括号,并使用.lower()
而不是,lower
应该是这样的
word = re.match("(0\w+)\W(0\w+)").lower()
,lower()
…。lower()
和suseprint word
谢谢,这已经消除了这个错误,但是现在我得到了:回溯(最近一次调用):文件“location.py”,第23行,在output=word\u replace(旧文本,replace\u dict)文件“location.py”,在word\u replace word=re.match(“(0\w+)\w(0\w+).lower())类型错误:match()至少接受2个参数(给定1个)是否确定这行word=re.match(((0\w+)\w(0\w+),lower())
?老实说,没有。我对这一切都很陌生。你会怎么做?你可能想尝试在函数中创建字典,而不是将其用作参数。我想我当时是这么做的:replace_dict={“value”:“new_value”,“value1”:“new_value1”,“value2”:“new_value2”,“value3”:“new_value3”}这已经解决了最初的错误,但现在我得到了:TypeError:match()接受了至少2个参数(1个给定)您是否尝试过word=re.match((0\w+)\w(0\w+),lower())
?是的,它说“NameError:global name'lower'没有定义”,所以我使用了。lower取而代之的是上面的TypeError
word = re.match("(0\w+)\W(0\w+)",lower()
word = re.match("(0\w+)\W(0\w+)").lower()