Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode a single string in python

What I want to do is:

I have a directory name and file name. I want to add them to a URL and make a URL call. Lets say the directory is

D:/somedir/userdata/scripts

and the file name is myscript.txt

I want to add these as parameters to URL call but I want to encode them separately and not as key value pair. What I want is a function which can take input like:

D:/somedir/userdata/scripts

and return output like:

D%3A%2Fsomedir%2Fuserdata%2Fscripts
like image 444
Vivek V Dwivedi Avatar asked May 26 '13 07:05

Vivek V Dwivedi


1 Answers

In Python 3:

>>> import urllib.parse
>>> urllib.parse.quote_plus('D:/somedir/userdata/scripts')
'D%3A%2Fsomedir%2Fuserdata%2Fscripts'

In Python 2:

>>> import urllib
>>> urllib.quote_plus('D:/somedir/userdata/scripts')
'D%3A%2Fsomedir%2Fuserdata%2Fscripts'
like image 75
mogul Avatar answered Sep 28 '22 11:09

mogul