On 24 Mai 2006, [EMAIL PROTECTED] wrote: > I have two Perl expressions > > > If windows: > > perl -ple "s/([^\w\s])/sprintf(q#%%%2X#, ord $1)/ge" somefile.txt > > If posix > > perl -ple 's/([^\w\s])/sprintf("%%%2X", ord $1)/ge' somefile.txt > > > > The [^\w\s] is a negated expression stating that any character > a-zA-Z0-9_, space or tab is ignored. > > The () captures whatever matches and throws it into the $1 for > processing by the sprintf > > In this case, %%%2X which is a three character hex value. > > How would you convert this to a python equivalent using the re or > similar module?
python -c "import re, sys;print re.sub(r'([^\w\s])', lambda s: '%%%2X' % ord(s.group()), sys.stdin.read())," < somefile It's not as short as the Perl version (and might have problems with big files). Python does not have such useful command line switches like -p (but you doesn't use Python so much for one liners as Perl) but it does the same ; at least in this special case (Python lacks something like the -l switch). With bash it's a bit easier. (maybe there's also a way with cmd.com to write multiple lines)? $ python -c "import re,sys for line in sys.stdin: print re.sub(r'([^\w\s])', lambda s: '%%%2X' % ord(s.group()), line)," < somefile Karl -- Please do *not* send copies of replies to me. I read the list _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor