简体   繁体   中英

How can I change urlencode to python dictionary

I have got data from POST like

first_name=jon&nick_name=harry

How can I change this to a python dictionary, like :

{
    "first_name":"jon",
    "nick_name":"harry"
}
>>> urlparse.parse_qs("first_name=jon&nick_name=harry")
{'nick_name': ['harry'], 'first_name': ['jon']}

If you trust that the URL arguments will always be properly formatted, this would be a minimal solution:

dict(pair.split("=") for pair in urlargs.split("&"))

In code that's going to be publicly accessible though, you'll probably want to use a library that does error checking. If you're using Django, you'll probably have access to them already in the dictionary-like object HttpRequest.POST.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM