Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-17-2009, 12:25 PM   #1
Registered: Oct 2006
Distribution: Debian x64
Posts: 198

Rep: Reputation: 30
Python: How to use the re module?

In Python if I want to use the re module with a file how would I do that? Open the file and read in with readlines is what I'm imagining:

for line in open('file1.csv').readlines():
        lines = [line.rstrip()]
        m = re.match('the string I want to match', lines)
but it doesn't work. I'm basically trying to emulate grep '^[0-9](6)'

And what would be the Python equivalent of sed 's/cat/dog/g'?
(I don't want to use sed or the OS module.)
Old 01-17-2009, 05:26 PM   #2
Senior Member
Registered: Nov 2005
Distribution: Arch
Posts: 3,163

Rep: Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370
Maybe you are over thinking this.

for line in open('in.txt').readlines():
    if re.match(r'^[0-9]\(6\)', line):
        print line,
Note that the regex syntax of python is like egrep rather than grep (thus the back-slashes).

And what would be the Python equivalent of sed 's/cat/dog/g'?
for line in open('in.txt').readlines():
    print re.sub('cat', 'dog', line),
Both these examples should probably use re.compile outside of the loop for efficiency.
1 members found this post helpful.
Old 01-19-2009, 12:26 PM   #3
Registered: Oct 2006
Distribution: Debian x64
Posts: 198

Original Poster
Rep: Reputation: 30
Thank you I ended up using:
g4 = open('workfile4', 'wb')

for line in open('workfile3').readlines():
    if re.match(r'^[0-9]{6}', line):
        studentid = re.match(r'^[0-9]{6}', line).group()
        print studentid, ",", line
        class1 = studentid, ",", line
        s4 =  str(class1)


To clean the file(s) up I used a lot of:
scriptutil.freplace('.', shellglobs=('workfile3',),regexl=((r'\\$',r'', None),))
scriptutil.freplace('.', shellglobs=('example',),regexl=((r'([A-Z])\,([A-Z])',r'\1\2', None),))
I'm not sure how efficient the scriptil function is, but I am only working with about one thousand lines of text.


grep, module, python, regex, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
python update - Unable to load GTK2 Python bindings: No module named gtk itpedersen Linux - Software 2 10-03-2008 03:44 AM
python module scope jhwilliams Programming 3 08-08-2007 03:16 PM
using distutils to install a python module shanenin Programming 2 11-29-2005 09:44 PM
no python module base.g? hcgernhardt Linux - Software 0 01-15-2005 12:16 PM
Help me (python and MySQLdb module) Dark Carnival Programming 2 04-22-2004 07:31 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:16 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration