python用input输入字典,表格表格作为字典输入python

I would like to create a html form to edit some database tables.

I tried to create a form with a table where in each table row is one entry of the database table like this:

name: Georgeage:
name: Jakeage:

But I cannot get the values into a dictionary or pandas dataframe so I can loop over it and update the table. It would even by possible to just get a list of the id's and a list of the ages and then loop over them (if they stay in the correct order).

The table in the database has the structure id, name, age.

Then need to set or adjust the age for all rows, so I want to update the ages by the id.

解决方案

You can use python-form-parser

from lxml import etree

from urllib import urlencode

html_string = """

"""

# parse and extract the form

tree = etree.HTML(html_string)

form = tree.xpath("//form[@id='my_form']")[0]

# Extract default values and meta information

ff = FormFiller(form)

# click to 'my_submit' button: will return dictionary with all fields values

form_data = ff.click('my_submit')

print form_data

{"my_field": "my_value",

"my_submit": "Submit the form"}

# fill user data

form_data['some_name'] = 'some_value'

# prefered way on how to fill user data:

print ff.fill('my_submit', my_field2="my_value2")

{"my_field": "my_value",

"my_field2": "my_value2",

"my_submit": "Submit the form"}

# fields that not present on the original form are not allowed

ff.fill('my_submit', field_not_present_in_form_name="my_value3")

KeyError: 'Invalid form field'

# prepare data for sending

body = urlencode(form_data)

url = ff.get_action_url("http://example.com/forms/my_form.html")

print url

'http://example.com/forms/my_path' # my_path from form's "action" attribute

method = ff.get_method()

print method

'POST' # from form's "method" attr (GET is default)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值