-1

I'm trying to use print function to print results of a re.match but it is coming back as invalid syntax for print

The python version is 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

Should come back and print results of word = re.match("(0\w+)\W(0\w+)",lower() but instead i get the below error:

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

3 Answers3

2

There is a missing bracket at the end of

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

it should be

    word = re.match("(0\w+)\W(0\w+)",lower())
Gaddi
  • 167
  • 1
  • 3
  • 16
  • Thanks, that has removed that error, but now i'm getting: Traceback (most recent call last): File "location.py", line 23, in output = word_replace(old_text, replace_dict) File "location.py", line 7, in word_replace word = re.match("(0\w+)\W(0\w+)".lower()) TypeError: match() takes at least 2 arguments (1 given) – OscarT Feb 13 '19 at 09:45
  • Are you sure about this line `word = re.match("(0\w+)\W(0\w+)",lower())` ? – Taohidul Islam Feb 13 '19 at 09:51
  • In all honesty, no. I'm fairly new to all this. What would you have done? – OscarT Feb 13 '19 at 09:53
  • You may want to try creating the dictionary in the function instead of using it as a parameter. – Gaddi Feb 13 '19 at 09:57
  • I thought I did that when i did: replace_dict = { "value" : 'new_value', "value1" : 'new_value1', "value2" : 'new_value2', "value3" : 'new_value3' } – OscarT Feb 13 '19 at 10:13
  • The dictionary would need to be mapped, the keys are found in the left hand column. You could potentially be better using a different data structure such as an array if you have no use in the keys. What are you trying to achieve in the end? – Gaddi Feb 13 '19 at 10:19
1

Change this :

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

into:

 word = re.match("(0\w+)\W(0\w+)",lower())
 print word
Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39
0
word = re.match("(0\w+)\W(0\w+)",lower()

this is wrong ,you forget to add closing bracket after quotes and use .lower() not ,lower

it should be like this

word = re.match("(0\w+)\W(0\w+)").lower()
Mayur Satav
  • 985
  • 2
  • 12
  • 32