简体   繁体   English

如何将urlencode更改为python词典

[英]How can I change urlencode to python dictionary

I have got data from POST like 我从POST获得数据

first_name=jon&nick_name=harry

How can I change this to a python dictionary, like : 如何将其更改为python字典,例如:

{
    "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: 如果您相信URL参数将始终正确设置格式,那么这将是一个最小的解决方案:

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. 如果您使用的是Django,则可能已经在类似字典的对象HttpRequest.POST中访问了它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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