[Tutor] Putting a system call into an re.sub()

Nick Lunt nick at javacat.f2s.com
Wed Feb 18 15:12:13 EST 2004


Hi list,

I'm trying to work out how to perform an re on some text, then when it
finds the search pattern to print out the whole line but make the
pattern highlighted using the tput command (linux system).

Here's a quick example of what I'm trying to achieve, where fl is any
file.

- code -

1. bold = os.system('tput smso')
2. norm = os.system('tput rmso')
3.
4. for line in fl.readlines():
5.   match = pat.search(line)
6.   if match:
7.      print pat.sub(pattern, bold + replace + norm,line)

-- end code --
 
Here's the output I'm getting:

- output -
print pat.sub(pattern, bold + replace + norm, line)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
- end output -

Which is understandable seen as I'm trying to add different types.

So I replaced the + signs with commas and got this:
- output -

- end output -
Traceback (most recent call last):
  File "./sed.py", line 29, in ?
    print pat.sub(replace, (bold, replace, norm), line)
TypeError: an integer is required

Obviously Im having problems here so any pointers would be appreciated.
I've studied the re docs on python.org and tried to find out more from
the book 'Text Processing in Python'.

It's simple to do in the shell and in perl, so I'm sure there's a simple
way in python ;)

Many thanks
Nick.



More information about the Tutor mailing list