Python, opposite function urllib.urlencode
How can I convert data after processing urllib.urlencode
to dict?
urllib.urldecode
does not exist.
python urllib
add a comment |
How can I convert data after processing urllib.urlencode
to dict?
urllib.urldecode
does not exist.
python urllib
add a comment |
How can I convert data after processing urllib.urlencode
to dict?
urllib.urldecode
does not exist.
python urllib
How can I convert data after processing urllib.urlencode
to dict?
urllib.urldecode
does not exist.
python urllib
python urllib
edited May 29 '16 at 10:44
guaka
10.8k74784
10.8k74784
asked Aug 22 '10 at 18:59
ArtyomArtyom
88821114
88821114
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
As the docs for urlencode
say,
The urlparse module provides the
functions parse_qs() and parse_qsl()
which are used to parse query strings
into Python data structures.
(In older Python releases, they were in the cgi
module). So, for example:
>>> import urllib
>>> import urlparse
>>> d = {'a':'b', 'c':'d'}
>>> s = urllib.urlencode(d)
>>> s
'a=b&c=d'
>>> d1 = urlparse.parse_qs(s)
>>> d1
{'a': ['b'], 'c': ['d']}
The obvious difference between the original dictionary d
and the "round-tripped" one d1
is that the latter has (single-item, in this case) lists as values -- that's because there is no uniqueness guarantee in query strings, and it may be important to your app to know about what multiple values have been given for each key (that is, the lists won't always be single-item ones;-).
As an alternative:
>>> sq = urlparse.parse_qsl(s)
>>> sq
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}
you can get a sequence of pairs (urlencode accepts such an argument, too -- in this case it preserves order, while in the dict case there's no order to preserve;-). If you know there are no duplicate "keys", or don't care if there are, then (as I've shown) you can call dict
to get a dictionary with non-list values. In general, however, you do need to consider what you want to do if duplicates are present (Python doesn't decide that on your behalf;-).
1
Alex Martelli, thank you!
– Artyom
Aug 22 '10 at 20:17
@Derek, you're welcome!
– Alex Martelli
Aug 22 '10 at 20:25
1
Very thorough answer. Awesome!
– Hartley Brody
Dec 21 '11 at 19:53
add a comment |
Python 3 code for Alex's solution:
>>> import urllib.parse
>>> d = {'a':'b', 'c':'d'}
>>> s = urllib.parse.urlencode(d)
>>> s
'a=b&c=d'
>>> d1 = urllib.parse.parse_qs(s)
>>> d1
{'a': ['b'], 'c': ['d']}
The alternative:
>>> sq = urllib.parse.parse_qsl(s)
>>> sq
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}
parse_qsl is reversible:
>>> urllib.parse.urlencode(sq)
'a=b&c=d'
add a comment |
urllib.unquote_plus()
does what you want. It Replace %xx escapes by their single-character equivalent and replaces plus signs with spaces.
Example:
unquote_plus('/%7Ecandidates/?name=john+connolly')
yields
'/~candidates/?name=john connolly'.
1
He said, he wanted a dict. So your answer is wrong.
– balrok
Jul 23 '14 at 16:45
4
yay, this is what I was looking for.
– Joe
Mar 21 '16 at 12:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3542881%2fpython-opposite-function-urllib-urlencode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
As the docs for urlencode
say,
The urlparse module provides the
functions parse_qs() and parse_qsl()
which are used to parse query strings
into Python data structures.
(In older Python releases, they were in the cgi
module). So, for example:
>>> import urllib
>>> import urlparse
>>> d = {'a':'b', 'c':'d'}
>>> s = urllib.urlencode(d)
>>> s
'a=b&c=d'
>>> d1 = urlparse.parse_qs(s)
>>> d1
{'a': ['b'], 'c': ['d']}
The obvious difference between the original dictionary d
and the "round-tripped" one d1
is that the latter has (single-item, in this case) lists as values -- that's because there is no uniqueness guarantee in query strings, and it may be important to your app to know about what multiple values have been given for each key (that is, the lists won't always be single-item ones;-).
As an alternative:
>>> sq = urlparse.parse_qsl(s)
>>> sq
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}
you can get a sequence of pairs (urlencode accepts such an argument, too -- in this case it preserves order, while in the dict case there's no order to preserve;-). If you know there are no duplicate "keys", or don't care if there are, then (as I've shown) you can call dict
to get a dictionary with non-list values. In general, however, you do need to consider what you want to do if duplicates are present (Python doesn't decide that on your behalf;-).
1
Alex Martelli, thank you!
– Artyom
Aug 22 '10 at 20:17
@Derek, you're welcome!
– Alex Martelli
Aug 22 '10 at 20:25
1
Very thorough answer. Awesome!
– Hartley Brody
Dec 21 '11 at 19:53
add a comment |
As the docs for urlencode
say,
The urlparse module provides the
functions parse_qs() and parse_qsl()
which are used to parse query strings
into Python data structures.
(In older Python releases, they were in the cgi
module). So, for example:
>>> import urllib
>>> import urlparse
>>> d = {'a':'b', 'c':'d'}
>>> s = urllib.urlencode(d)
>>> s
'a=b&c=d'
>>> d1 = urlparse.parse_qs(s)
>>> d1
{'a': ['b'], 'c': ['d']}
The obvious difference between the original dictionary d
and the "round-tripped" one d1
is that the latter has (single-item, in this case) lists as values -- that's because there is no uniqueness guarantee in query strings, and it may be important to your app to know about what multiple values have been given for each key (that is, the lists won't always be single-item ones;-).
As an alternative:
>>> sq = urlparse.parse_qsl(s)
>>> sq
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}
you can get a sequence of pairs (urlencode accepts such an argument, too -- in this case it preserves order, while in the dict case there's no order to preserve;-). If you know there are no duplicate "keys", or don't care if there are, then (as I've shown) you can call dict
to get a dictionary with non-list values. In general, however, you do need to consider what you want to do if duplicates are present (Python doesn't decide that on your behalf;-).
1
Alex Martelli, thank you!
– Artyom
Aug 22 '10 at 20:17
@Derek, you're welcome!
– Alex Martelli
Aug 22 '10 at 20:25
1
Very thorough answer. Awesome!
– Hartley Brody
Dec 21 '11 at 19:53
add a comment |
As the docs for urlencode
say,
The urlparse module provides the
functions parse_qs() and parse_qsl()
which are used to parse query strings
into Python data structures.
(In older Python releases, they were in the cgi
module). So, for example:
>>> import urllib
>>> import urlparse
>>> d = {'a':'b', 'c':'d'}
>>> s = urllib.urlencode(d)
>>> s
'a=b&c=d'
>>> d1 = urlparse.parse_qs(s)
>>> d1
{'a': ['b'], 'c': ['d']}
The obvious difference between the original dictionary d
and the "round-tripped" one d1
is that the latter has (single-item, in this case) lists as values -- that's because there is no uniqueness guarantee in query strings, and it may be important to your app to know about what multiple values have been given for each key (that is, the lists won't always be single-item ones;-).
As an alternative:
>>> sq = urlparse.parse_qsl(s)
>>> sq
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}
you can get a sequence of pairs (urlencode accepts such an argument, too -- in this case it preserves order, while in the dict case there's no order to preserve;-). If you know there are no duplicate "keys", or don't care if there are, then (as I've shown) you can call dict
to get a dictionary with non-list values. In general, however, you do need to consider what you want to do if duplicates are present (Python doesn't decide that on your behalf;-).
As the docs for urlencode
say,
The urlparse module provides the
functions parse_qs() and parse_qsl()
which are used to parse query strings
into Python data structures.
(In older Python releases, they were in the cgi
module). So, for example:
>>> import urllib
>>> import urlparse
>>> d = {'a':'b', 'c':'d'}
>>> s = urllib.urlencode(d)
>>> s
'a=b&c=d'
>>> d1 = urlparse.parse_qs(s)
>>> d1
{'a': ['b'], 'c': ['d']}
The obvious difference between the original dictionary d
and the "round-tripped" one d1
is that the latter has (single-item, in this case) lists as values -- that's because there is no uniqueness guarantee in query strings, and it may be important to your app to know about what multiple values have been given for each key (that is, the lists won't always be single-item ones;-).
As an alternative:
>>> sq = urlparse.parse_qsl(s)
>>> sq
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}
you can get a sequence of pairs (urlencode accepts such an argument, too -- in this case it preserves order, while in the dict case there's no order to preserve;-). If you know there are no duplicate "keys", or don't care if there are, then (as I've shown) you can call dict
to get a dictionary with non-list values. In general, however, you do need to consider what you want to do if duplicates are present (Python doesn't decide that on your behalf;-).
edited Aug 22 '10 at 19:09
answered Aug 22 '10 at 19:02
Alex MartelliAlex Martelli
629k12810411281
629k12810411281
1
Alex Martelli, thank you!
– Artyom
Aug 22 '10 at 20:17
@Derek, you're welcome!
– Alex Martelli
Aug 22 '10 at 20:25
1
Very thorough answer. Awesome!
– Hartley Brody
Dec 21 '11 at 19:53
add a comment |
1
Alex Martelli, thank you!
– Artyom
Aug 22 '10 at 20:17
@Derek, you're welcome!
– Alex Martelli
Aug 22 '10 at 20:25
1
Very thorough answer. Awesome!
– Hartley Brody
Dec 21 '11 at 19:53
1
1
Alex Martelli, thank you!
– Artyom
Aug 22 '10 at 20:17
Alex Martelli, thank you!
– Artyom
Aug 22 '10 at 20:17
@Derek, you're welcome!
– Alex Martelli
Aug 22 '10 at 20:25
@Derek, you're welcome!
– Alex Martelli
Aug 22 '10 at 20:25
1
1
Very thorough answer. Awesome!
– Hartley Brody
Dec 21 '11 at 19:53
Very thorough answer. Awesome!
– Hartley Brody
Dec 21 '11 at 19:53
add a comment |
Python 3 code for Alex's solution:
>>> import urllib.parse
>>> d = {'a':'b', 'c':'d'}
>>> s = urllib.parse.urlencode(d)
>>> s
'a=b&c=d'
>>> d1 = urllib.parse.parse_qs(s)
>>> d1
{'a': ['b'], 'c': ['d']}
The alternative:
>>> sq = urllib.parse.parse_qsl(s)
>>> sq
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}
parse_qsl is reversible:
>>> urllib.parse.urlencode(sq)
'a=b&c=d'
add a comment |
Python 3 code for Alex's solution:
>>> import urllib.parse
>>> d = {'a':'b', 'c':'d'}
>>> s = urllib.parse.urlencode(d)
>>> s
'a=b&c=d'
>>> d1 = urllib.parse.parse_qs(s)
>>> d1
{'a': ['b'], 'c': ['d']}
The alternative:
>>> sq = urllib.parse.parse_qsl(s)
>>> sq
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}
parse_qsl is reversible:
>>> urllib.parse.urlencode(sq)
'a=b&c=d'
add a comment |
Python 3 code for Alex's solution:
>>> import urllib.parse
>>> d = {'a':'b', 'c':'d'}
>>> s = urllib.parse.urlencode(d)
>>> s
'a=b&c=d'
>>> d1 = urllib.parse.parse_qs(s)
>>> d1
{'a': ['b'], 'c': ['d']}
The alternative:
>>> sq = urllib.parse.parse_qsl(s)
>>> sq
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}
parse_qsl is reversible:
>>> urllib.parse.urlencode(sq)
'a=b&c=d'
Python 3 code for Alex's solution:
>>> import urllib.parse
>>> d = {'a':'b', 'c':'d'}
>>> s = urllib.parse.urlencode(d)
>>> s
'a=b&c=d'
>>> d1 = urllib.parse.parse_qs(s)
>>> d1
{'a': ['b'], 'c': ['d']}
The alternative:
>>> sq = urllib.parse.parse_qsl(s)
>>> sq
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}
parse_qsl is reversible:
>>> urllib.parse.urlencode(sq)
'a=b&c=d'
answered Apr 17 '12 at 0:02
phobiephobie
1,61511519
1,61511519
add a comment |
add a comment |
urllib.unquote_plus()
does what you want. It Replace %xx escapes by their single-character equivalent and replaces plus signs with spaces.
Example:
unquote_plus('/%7Ecandidates/?name=john+connolly')
yields
'/~candidates/?name=john connolly'.
1
He said, he wanted a dict. So your answer is wrong.
– balrok
Jul 23 '14 at 16:45
4
yay, this is what I was looking for.
– Joe
Mar 21 '16 at 12:01
add a comment |
urllib.unquote_plus()
does what you want. It Replace %xx escapes by their single-character equivalent and replaces plus signs with spaces.
Example:
unquote_plus('/%7Ecandidates/?name=john+connolly')
yields
'/~candidates/?name=john connolly'.
1
He said, he wanted a dict. So your answer is wrong.
– balrok
Jul 23 '14 at 16:45
4
yay, this is what I was looking for.
– Joe
Mar 21 '16 at 12:01
add a comment |
urllib.unquote_plus()
does what you want. It Replace %xx escapes by their single-character equivalent and replaces plus signs with spaces.
Example:
unquote_plus('/%7Ecandidates/?name=john+connolly')
yields
'/~candidates/?name=john connolly'.
urllib.unquote_plus()
does what you want. It Replace %xx escapes by their single-character equivalent and replaces plus signs with spaces.
Example:
unquote_plus('/%7Ecandidates/?name=john+connolly')
yields
'/~candidates/?name=john connolly'.
edited Nov 15 '18 at 8:11
Jean-François Corbett
28.8k22110161
28.8k22110161
answered Feb 26 '14 at 15:36
Andrew FarrellAndrew Farrell
1,3131218
1,3131218
1
He said, he wanted a dict. So your answer is wrong.
– balrok
Jul 23 '14 at 16:45
4
yay, this is what I was looking for.
– Joe
Mar 21 '16 at 12:01
add a comment |
1
He said, he wanted a dict. So your answer is wrong.
– balrok
Jul 23 '14 at 16:45
4
yay, this is what I was looking for.
– Joe
Mar 21 '16 at 12:01
1
1
He said, he wanted a dict. So your answer is wrong.
– balrok
Jul 23 '14 at 16:45
He said, he wanted a dict. So your answer is wrong.
– balrok
Jul 23 '14 at 16:45
4
4
yay, this is what I was looking for.
– Joe
Mar 21 '16 at 12:01
yay, this is what I was looking for.
– Joe
Mar 21 '16 at 12:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3542881%2fpython-opposite-function-urllib-urlencode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown