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

Nick Lunt nick at javacat.f2s.com
Thu Feb 19 11:52:14 EST 2004


Thanks Magnus and Herschel for your help.

I'm still having problems but with the info supplied by yourselves I
should get it sorted soon.

Many thanks again,
Nick.


On Thu, 19 Feb 2004 10:29:09 +0100 Magnus Lycka <magnus at thinkware.se>
wrote:

> You seem to be confusing os.system('tput smso') with
> os.popen('tput smso').read(). os.system only returns
> an exit status. That's the reason that you get that
> integer + string error. os.popen returns a file
> handle containing the output of your command.
> 
> On the other hand, you don't really need tput. You just 
> insert ANSI color codes, right? See e.g. these references:
> http://mail.python.org/pipermail/tutor/2003-January/019588.html
> http://pueblo.sourceforge.net/doc/manual/ansi_color_codes.html
> 
> 
> -----Ursprungligt meddelande-----
> Från: Nick Lunt <nick at javacat.f2s.com>
> Skickat: 2004-02-18  21:12:13
> Till: tutor at python.org
> Ämne: [Tutor] Putting a system call into an re.sub()
> 
> 
> > 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.
> > 
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> > 
> 
> 
> -- 
> Magnus Lycka, Thinkware AB
> Alvans vag 99, SE-907 50 UMEA, SWEDEN
> phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
> http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list