[Tutor] Question on regular expressions

Kent Johnson kent37 at tds.net
Thu May 25 14:22:52 CEST 2006


Andrew Robert wrote:
> The python method inserts extra blank lines after each hex value line.
> Does anyone know why this might be?
> 
> Is the print statement inserting a artificial new line character?

Yes, this is a feature of print, it always inserts a newline. To avoid 
this, use sys.stdout.write() instead of print:
for line i open(r'e:\pycode\sigh.txt','rb'):
     line = re.sub(r'([^\w\s])', lambda s: '%%%2X' % ord(s.group()), line)
     sys.stdout.write(line)

Kent

> 
> If so, how cam I remove that?
> 
> 
> The python code I am using is:
> 
> 
> 
> import re,sys
> 
> for line i open(r'e:\pycode\sigh.txt','rb'):
>     print re.sub(r'([^\w\s])', lambda s: '%%%2X' % ord(s.group()), line)



More information about the Tutor mailing list