regex line by line over file

James Smith bjlockie at
Thu Mar 27 04:23:29 CET 2014

I can't get this to work.
It runs but there is no output when I try it on a file.


import os
import sys
import re
from datetime import datetime

#logDir = '/nfs/projects/equinox/platformTools/RTLG/RTLG_logs';
#os.chdir( logDir );

programName = sys.argv[0]
fileName = sys.argv[1]

#pattern = re.compile('\s*\\"SHELF-.*,SC,.*,:\\"Log Collection In Progress\\"')
p = re.compile('^\s*\"SHELF-.*,SC,.*,:\\\"Log Collection In Progress\\\"')
l = '    "SHELF-17:LOG_COLN_IP,SC,03-25,01-18-58,NEND,NA,,,:\"Log Collection In Progress\",NONE:1700000035-6364-1048,:YEAR=2014,MODE=NONE"'

# this works :-)
m = p.match( l )
if m:
    print( l )

# this doesn't match anything (or the if doesn't work) :-(
with open(fileName) as f:
    for line in f:
        # debug code (print the line without adding a linefeed)
        # sys.stdout.write( line )
        if p.match(line):

The test file just has one line:
    "SHELF-17:LOG_COLN_IP,SC,03-25,01-18-58,NEND,NA,,,:\"Log Collection In Progress\",NONE:1700000035-6364-1048,:YEAR=2014,MODE=NONE"

More information about the Python-list mailing list