URL Parsing Utility?
will
willg at bluesock.org
Mon Apr 8 18:54:37 EDT 2002
There are a couple of ways of solving the vertical whitespace problem.
You're getting two carriage returns from:
print ick
The first comes from the actual data which has a carriage return at the
end. The second comes from the print function which appends a carriage
return to the text.
If you don't want either of them you could do:
print ick[:-1],
or alternatively:
sys.stdout.write(ick[:-1])
The first one might append a space at the end of ick[:-1]--I'm not
entirely sure.
I use Perl for string munging where I would have once used sed and awk.
And sometimes I use it for reporting as you do. Perl one-liners are
really powerful and just make my day. I also use Perl where I need to do
setuid root kinds of things because to do that in Python you have to write
a wrapper in C.
On Mon, 8 Apr 2002, John Chambers wrote:
> While we're doing a python lesson, I'd be interested in how to solve
> a problem with this code that I've seen with a lot of python, and
> which doesn't seem to be covered too well in TFM. Maybe it's just too
> trivial. The problem is that, when given the URL on stdin, this
> program produces two lines of output, not one. The second line is
> blank. This is, of course, silly, but lack of information on exactly
> how to get such trivia correct can be a significant barier. I tend to
> continue using perl, because when the input needs to be fed to some
> other program that's picky about its input, I can control the white
> space exactly in perl. With python, I can get the data right, but I
> always seem to get silly extra white space like this, and I don't
> have a good enough handle on python's char handling to understand
> where it's coming from or how to Get it Right.
>
> | #!/usr/bin/env python
> | import sys, urllib
> |
> | if len(sys.argv) > 1:
> | ick = sys.argv[1]
> | else:
> | ick = sys.stdin.readline()
> |
> | try:
> | print urllib.unquote(ick)
> | except:
> | print "could not be unquoted."
>
>
> jc at trillian.mit.edu, the John Chambers who long ago learned that anal
> retentiveness is a required characteristic of a good programmer.
>
> (And I've long argued that the most significant technical advance in
> perl5 was the chomp function. ;-)
--
whatever it is, you can find it at http://www.bluesock.org/~willg/
except Will--you can only see him in real life.
More information about the Discuss
mailing list