Posts: 10
Threads: 2
Joined: Oct 2017
(Oct-27-2017, 03:35 PM)heiner55 Wrote: print("Line ", linenr, ": ", match[1], sep='')
I typed the following and received the "SyntaxError: invalid syntax", with the '=' sign highlighted in red in the line print("Line", liner, ":", match[1], sep='')
"
>>> pattern = re.compile(r"tel:\s*?\+44\s*?(\d{10,10})")
>>> with open ('phone_log.txt') as in_file:
for linenr, line in enumerate(in_file):
match = re.search(pattern, line, re.X)
if match:
print("Line", liner, ":", match[1], sep='')
SyntaxError: invalid syntax
>>> "
Posts: 606
Threads: 3
Joined: Nov 2016
Oct-27-2017, 03:48 PM
(This post was last modified: Oct-27-2017, 03:48 PM by heiner55.)
Then remove sep=''':
print("Line ", linenr, ": ", match[1])
Posts: 10
Threads: 2
Joined: Oct 2017
(Oct-27-2017, 03:48 PM)heiner55 Wrote: Then remove sep=''':
print("Line ", linenr, ": ", match[1])
I tried doing this earlier but received the following :
>>> with open ('phone_log.txt') as in_file:
for linenr, line in enumerate(in_file):
match = re.search(pattern, line, re.X)
if match:
print("Line ", linenr, ": ", match[1])
Traceback (most recent call last):
File "<pyshell#20>", line 3, in <module>
match = re.search(pattern, line, re.X)
File "C:\Python27\lib\re.py", line 142, in search
return _compile(pattern, flags).search(string)
File "C:\Python27\lib\re.py", line 235, in _compile
raise ValueError('Cannot process flags argument with a compiled pattern')
ValueError: Cannot process flags argument with a compiled pattern
Posts: 606
Threads: 3
Joined: Nov 2016
Oct-27-2017, 04:01 PM
(This post was last modified: Oct-27-2017, 04:30 PM by heiner55.)
My sample runs with Python 3.6 (not with Python 2.7).
Uninstall your Python 2.7 and install Python 3.6:
https://www.python.org/ftp/python/3.6.3/...-3.6.3.exe
As a new beginner you use Python 3.6.
This code runs with Python 3.6 or Python 2.7:
#!/usr/bin/python3
import re
pattern = r"""
tel: # tel:
\s*? # maybe some spaces
\+44 # +44
\s*? # maybe some spaces
(\d{10,10}) # 10 digits
"""
with open ('phone_log.txt') as in_file:
for linenr, line in enumerate(in_file):
match = re.search(pattern, line, re.X)
if match:
print("Line %d: %s" % (linenr, match.group(1)))
Posts: 10
Threads: 2
Joined: Oct 2017
(Oct-27-2017, 04:01 PM)heiner55 Wrote: My sample runs with Python 3.6 (not with Python 2.7).
Uninstall your Python 2.7 and install Python 3.6:
https://www.python.org/ftp/python/3.6.3/...-3.6.3.exe
As a new beginner you use Python 3.6.
This code runs with Python 3.6 or Python 2.7:
#!/usr/bin/python3
import re
pattern = r"""
tel: # tel:
\s*? # maybe some spaces
\+44 # +44
\s*? # maybe some spaces
(\d{10,10}) # 10 digits
"""
with open ('phone_log.txt') as in_file:
for linenr, line in enumerate(in_file):
match = re.search(pattern, line, re.X)
if match:
print("Line %d: %s" % (linenr, match.group(1)))
Thanks mate, I've done that now..
But still getting an error, I get the following:
>>> import re
>>> pattern=re.compile(r'''tel:\s*?\+44\s*?(\d{10,10})''')
>>> with open ('phone_log.txt') as in_file:
for linenr, line in enumerate(in_file):
match = re.search(pattern, line, re.X)
if match:
print("Line ", linenr, ": ", match[1])
Traceback (most recent call last):
File "<pyshell#11>", line 3, in <module>
match = re.search(pattern, line, re.X)
File "C:\Users\Ronnie\AppData\Local\Programs\Python\Python36-32\lib\re.py", line 182, in search
return _compile(pattern, flags).search(string)
File "C:\Users\Ronnie\AppData\Local\Programs\Python\Python36-32\lib\re.py", line 297, in _compile
"cannot process flags argument with a compiled pattern")
ValueError: cannot process flags argument with a compiled pattern
I was wondering how I can get rid of this?
Posts: 606
Threads: 3
Joined: Nov 2016
Oct-27-2017, 04:44 PM
(This post was last modified: Oct-27-2017, 04:44 PM by heiner55.)
Your program is not the same as mine.
Paste my sample and run it.
#!/usr/bin/python3
import re
pattern = r"""
tel: # tel:
\s*? # maybe some spaces
\+44 # +44
\s*? # maybe some spaces
(\d{10,10}) # 10 digits
"""
with open ('phone_log.txt') as in_file:
for linenr, line in enumerate(in_file):
match = re.search(pattern, line, re.X)
if match:
print("Line %d: %s" % (linenr, match.group(1)))
#done
Posts: 10
Threads: 2
Joined: Oct 2017
Oct-27-2017, 05:01 PM
(This post was last modified: Oct-27-2017, 05:01 PM by Ronnie.)
(Oct-27-2017, 04:44 PM)heiner55 Wrote: Your program is not the same as mine.
Paste my sample and run it.
#!/usr/bin/python3
import re
pattern = r"""
tel: # tel:
\s*? # maybe some spaces
\+44 # +44
\s*? # maybe some spaces
(\d{10,10}) # 10 digits
"""
with open ('phone_log.txt') as in_file:
for linenr, line in enumerate(in_file):
match = re.search(pattern, line, re.X)
if match:
print("Line %d: %s" % (linenr, match.group(1)))
#done
So I should copy:
#!/usr/bin/python3
import re
pattern = r"""
tel: # tel:
\s*? # maybe some spaces
\+44 # +44
\s*? # maybe some spaces
(\d{10,10}) # 10 digits
"""
with open ('phone_log.txt') as in_file:
for linenr, line in enumerate(in_file):
match = re.search(pattern, line, re.X)
if match:
print("Line %d: %s" % (linenr, match.group(1)))
#done
into my shell and then run it?
and what run option do i select, since there is
- run python
- run shell
and is the re.compile needed at all? since that is missing
Posts: 606
Threads: 3
Joined: Nov 2016
Oct-27-2017, 05:19 PM
(This post was last modified: Oct-27-2017, 05:19 PM by heiner55.)
You should copy it into a file (sample.py).
Then open IDLE
Then Menu->File->Open->sample.py
A new window is open.
Then F5 or Run-> Run Module
Or open a cmd-Window and start: python.exe sample.py
(assuming python.exe is in PATH)
Posts: 7,319
Threads: 123
Joined: Sep 2016
Ronnie you got information how to use BBcode tag in first post.
Use code tags,more posts without will not be tolerated.
|