pythonic parsing of URL
Green.Horn.000 at gmail.com
Mon Jul 30 18:46:27 CEST 2007
On Jul 27, 10:04 pm, Steven D'Aprano
<st... at REMOVE.THIS.cybersource.com.au> wrote:
> On Sat, 28 Jul 2007 03:10:32 +0000, GreenH wrote:
> > I get some string as below from a library method (qt3
> > QDropEvent.data()) I use.
> > file:///C:/Documents%20and%20Settings/Username/My%20Documents/45-61-Abc%20fold-%20den.vru
> > I need file path on my system, for the above example:
> > C:/Documents and Settings/Username/My Documents/45-61-Abc fold-
> > den.vru
> > I am doing the below, it doesn't look pythonic,
> > can someone suggest any elegant solution, which is not too cryptic to
> > understand? yep, I do care about readability of the code to average
> > python user :)
> Try this:
> import urlparse, sys
> fileURLname = ("file:///C:/Documents%20and%20Settings/Username/"
> pathname = urlparse.urlparse(fileURLname)
> if sys.platform == "win32":
> import nturl2path
> clean = nturl2path.url2pathname(pathname)
> import urllib
> clean = urllib.unquote(pathname)
> print clean
> > --------------------
> > tmpTuple = urlparse.urlparse(fileURLname)
> > tmpString = tmpTuple.strip('/\\')
> > fileName = urllib.unquote(tmpString)
> > #For some reason the string contained in 'fileName' has some
> > unprintable trailing characters, Any ideas on that?
> It works for me: No trailing characters at all, printable or otherwise.
> >>> len(fileName.split('.vru', 1))
Thanks for the reply. The unprintable character was null byte.
More information about the Python-list