Python 解析来自web.py的响应
我正在尝试学习web.py以获得一个表单,在该表单中,我可以获取响应并使用响应在函数中执行某些操作,不幸的是,返回的响应的格式如下:Python 解析来自web.py的响应,python,web.py,Python,Web.py,我正在尝试学习web.py以获得一个表单,在该表单中,我可以获取响应并使用响应在函数中执行某些操作,不幸的是,返回的响应的格式如下: <Storage {'Domain required': u'wing.com', 'minutes needed': u'1', 'Submit': u''}> 我可以写一些东西来解析,但肯定有一些内置的方法来解析它。很明显我遗漏了什么。我怎样才能得到我可爱的回答,坐在变量、列表或dict中 提前感谢。下面是一个简单的示例,它显示了访问存储对象
<Storage {'Domain required': u'wing.com', 'minutes needed': u'1', 'Submit': u''}>
我可以写一些东西来解析,但肯定有一些内置的方法来解析它。很明显我遗漏了什么。我怎样才能得到我可爱的回答,坐在变量、列表或dict中
提前感谢。下面是一个简单的示例,它显示了访问存储对象中的值的各种方式 基本上,存储对象可以看作是一个奇特的dict。它允许您像dict一样访问值,但也可以作为属性访问值 例外情况是变量名称中有空格,如“Domain required”。在这种情况下,您需要使用dict表示法进行访问 这里还有一个指向存储类实现的链接(主要的收获是它是dict的一个子类): 属性访问的用法示例:
print response['Domain required']
print response['minutes needed']
print response.Submit
循环使用示例(就像dict):
示例web.py类:
class SomePage(object):
def GET(self):
web.header("Content-Type", "text/html")
return """
<html><body>
<form method="POST">
<input type="input" name="Domain required" />
<input type="input" name="example_field" />
<input type="input" name="example_field_2" />
<input type="submit" name="submit"/>
</form>
</body></html>
"""
def POST(self):
cgi_fields = web.input()
return cgi_fields['Domain required'], cgi_fields.example_field, cgi_fields.get('example_field_2')
class SomePage(对象):
def GET(自我):
标题(“内容类型”、“文本/html”)
返回“”
"""
def POST(自我):
cgi_fields=web.input()
返回cgi_字段['Domain required'],cgi_字段.example_字段,cgi_字段.get('example_字段\u 2')
请将您的程序缩减为再现您的问题的最小完整测试用例,并将该程序复制粘贴到您的问题中。rob,这不是问题,这是表单输出,输出不是问题,我只是想把它解析成变量。对,但是要想知道你遗漏了什么信息,就要知道你已经理解了什么。这是通过提供一个程序来实现的,该程序可以正确运行,直到您开始混淆为止。存储对象可以像dict一样访问。我假设它也可以像dict一样访问。您还可以作为属性访问值。例如响应。提交。然而,我不确定如何访问“需要域”,因为我从未尝试过。可能是响应。需要域\u
谢谢Gohn!我去看看
class SomePage(object):
def GET(self):
web.header("Content-Type", "text/html")
return """
<html><body>
<form method="POST">
<input type="input" name="Domain required" />
<input type="input" name="example_field" />
<input type="input" name="example_field_2" />
<input type="submit" name="submit"/>
</form>
</body></html>
"""
def POST(self):
cgi_fields = web.input()
return cgi_fields['Domain required'], cgi_fields.example_field, cgi_fields.get('example_field_2')