Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting numbers
#11
(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
>>> "
Reply
#12
Then remove sep=''':

print("Line ", linenr, ": ", match[1])
Reply
#13
(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
Reply
#14
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)))
Reply
#15
(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?
Reply
#16
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
Reply
#17
(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
Reply
#18
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)
Reply
#19
Ronnie you got information how to use BBcode tag in first post.
Use code tags,more posts without will not be tolerated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,668 May-09-2019, 12:19 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020