User Tools

Site Tools


wiki:python:basicpython4

Use Python for pattern searching


Material prepared by Stephen Weston

Lecture Introduction to Python

mkdir python
cd python
wget http://research.computing.yale.edu/sites/default/files/files/python13JUL2016.pdf
wget https://raw.githubusercontent.com/selvaje/spatial-ecology-codes/master/wiki/python/notebooks/Python_Notebook.ipynb

Exercise

wget https://raw.githubusercontent.com/selvaje/spatial-ecology-codes/master/wiki/python/example.txt
wget https://raw.githubusercontent.com/selvaje/spatial-ecology-codes/master/wiki/python/grep_1.py
wget https://raw.githubusercontent.com/selvaje/spatial-ecology-codes/master/wiki/python/grep_2.py
wget https://raw.githubusercontent.com/selvaje/spatial-ecology-codes/master/wiki/python/grep_3.py
wget https://raw.githubusercontent.com/selvaje/spatial-ecology-codes/master/wiki/python/grep.py
wget https://raw.githubusercontent.com/selvaje/spatial-ecology-codes/master/wiki/python/sub.py
wget https://raw.githubusercontent.com/selvaje/spatial-ecology-codes/master/wiki/python/grep_color.py
./grep_1.py
wiki/python/grep_1.py
#!/usr/bin/env python
#
# Print all lines from "example.txt" that contain the string "line"
#
import sys, re
for s in open('example.txt', 'rt'):
    if re.search('line', s):
        sys.stdout.write(s)
./grep_2.py line example.txt
wiki/python/grep_2.py
#!/usr/bin/env python
#
# Print all lines from file argv[2] that match regex argv[1]
#
import sys, re
for s in open(sys.argv[2], 'rt'):
    if re.search(sys.argv[1], s):
        sys.stdout.write(s)
./grep_3.py line example.txt
wiki/python/grep_3.py
#!/usr/bin/env python
#
# Python script that works like a simplified "grep" command
# but without any error handling
#
import sys, re
 
for fn in sys.argv[2:]:
    fobj = open(fn, 'rt')
    for s in fobj:
        if re.search(sys.argv[1], s):
            sys.stdout.write(s)
    fobj.close()
# see the error message 
./grep.py line
# correct way to run 
./grep.py line example.txt
wiki/python/grep.py
#!/usr/bin/env python
#
# Python script that works like a simplified "grep" command
# which doesn't read from stdin
#
import sys, re
from os.path import basename
 
if len(sys.argv) < 3:
    sys.stderr.write('Usage: %s regex file ...\n' % basename(sys.argv[0]))
    sys.exit(1)
 
r = re.compile(sys.argv[1])
 
for fn in sys.argv[2:]:
    try:
        fobj = open(fn, 'rt')
        try:
            for s in fobj:
                if r.search(s):
                    sys.stdout.write(s)
        finally:
            fobj.close()
    except IOError, e:
        sys.stderr.write('error opening file %r\n' % fn)
wiki/python/sub.py
#!/usr/bin/env python
#
# Example use of the re.sub function
#
import re
print re.sub('a+', '++\\g<0>++', 'a test aaaa of aaaaaa')
./grep_color.py line example.txt
# or 
./grep_color.py  "t[a-z]"   example.txt
wiki/python/grep_color.py
#!/usr/bin/env python
#
# Python script that works like a simplified "grep" command
# which doesn't read from stdin and uses terminal highlighting
#
import sys, re
from os.path import basename
 
if len(sys.argv) < 3:
    sys.stderr.write('Usage: %s regex file ...\n' % basename(sys.argv[0]))
    sys.exit(1)
 
r = re.compile(sys.argv[1])
repl = '\x1b[7m\\g<0>\x1b[0m'
 
for fn in sys.argv[2:]:
    try:
        fobj = open(fn, 'rt')
        try:
            for s in fobj:
                if r.search(s):
                    sys.stdout.write(re.sub(r, repl, s))
        finally:
            fobj.close()
    except IOError, e:
        sys.stderr.write('error opening file %r\n' % fn)
wiki/python/basicpython4.txt · Last modified: 2020/07/17 06:39 (external edit)