pythonic parsing of URL

G

GreenH

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 :)

--------------------
tmpTuple = urlparse.urlparse(fileURLname)

tmpString = tmpTuple[2].strip('/\\')

fileName = urllib.unquote(tmpString)

#For some reason the string contained in 'fileName' has some
unprintable trailing characters, Any ideas on that?
#thus I do below

fileName = fileName.split('.vru')[0] + '.vru'
 
S

Steven D'Aprano

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/"
"My%20Documents/45-61-Abc%20fold-%20den.vru")
pathname = urlparse.urlparse(fileURLname)[2]
if sys.platform == "win32":
import nturl2path
clean = nturl2path.url2pathname(pathname)
else:
import urllib
clean = urllib.unquote(pathname)
print clean

--------------------
tmpTuple = urlparse.urlparse(fileURLname)

tmpString = tmpTuple[2].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)[1])
0
 
G

GreenH

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/"
"My%20Documents/45-61-Abc%20fold-%20den.vru")
pathname = urlparse.urlparse(fileURLname)[2]
if sys.platform == "win32":
import nturl2path
clean = nturl2path.url2pathname(pathname)
else:
import urllib
clean = urllib.unquote(pathname)
print clean
tmpString = tmpTuple[2].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)[1])

0

Hi!
Thanks for the reply. The unprintable character was null byte.

-Greene.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top