[Tutor] Question on regular expressions
Andrew Robert
andrew.arobert at gmail.com
Thu May 25 01:51:12 CEST 2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Wow!!..
That awesome!
My goal was not to make it a one-liner per-se..
I was simply trying to show the functionality I was trying to duplicate.
Boiling your one-liner down into a multi-line piece of code, I did:
#!c:\python24\python
import re,sys
a = open(r'e:\pycode\csums.txt','rb').readlines()
for line in a:
print re.sub(r'([^\w\s])', lambda s: '%%%2X' % ord(s.group()), line)
Breaking down the command, you appear to be calling an un-named function
to act against any characters trapped by the regular expression.
Not familiar with lamda :).
The un-named function does in-place transformation of the character to
the established hex value.
Does this sound right?
If I then saved the altered output to a file and wanted to transform it
back to its original form, I would do the following in perl.
perl -ple 's/(?:%([0-9A-F]{2}))/chr hex $1/eg' somefiletxt
How would you reverse the process from a python point of view?
<snip>
</snip>
Karl Pflästerer wrote:
> 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
- --
Thank you,
Andrew Robert
Systems Architect
Information Technologies
MFS Investment Management
Phone: 617-954-5882
E-mail: arobert at mfs.com
Linux User Number: #201204
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
iD8DBQFEdPFwDvn/4H0LjDwRAuzuAKCOPja9Js1ueP2GoT+B0hoFubDEegCguzfT
QL87gmKUx6znmGQxXqg6V+A=
=7MT2
-----END PGP SIGNATURE-----
More information about the Tutor
mailing list