表格表格作为字典输入python [英] form table as dictionary input python

查看:124
本文介绍了表格表格作为字典输入python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个html表单来编辑一些数据库表.

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:

<form action="/adjust_table.py" method="post">
  <table>
    <tr>
    <td colspan = 4><input type="text" name="id" value="123" readonly></td>
    <td colspan = 4>name: George</td>
    <td colspan = 4>age: <input type="text" name="age[123]" value="73"></td>
    </tr>
    <tr>
    <td colspan = 4><input type="text" name="id" value="1269" readonly></td>
    <td colspan = 4>name: Jake</td>
    <td colspan = 4>age: <input type="text" name="age[1269]" value="26"></td>
    </tr>
    <input type="submit" value="Submit">
  </table>
</form>

但是我无法将值放入字典或pandas数据框中,因此可以循环遍历并更新表.甚至有可能只获取ID的列表和年龄的列表,然后在它们上循环(如果它们以正确的顺序排列).

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).

数据库中的表具有结构ID,名称,年龄. 然后需要设置或调整所有行的年龄,所以我想通过ID更新年龄.

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.

推荐答案

您可以使用 python-form-parser

from lxml import etree
from urllib import urlencode

html_string = """
    <html>
        <body>
            <form action="my_path", method="POST" id="my_form">
                  <input type="text" name="my_field" value="my_value"/>
                  <input type="text" name="my_field2"/>
                  <input type="submit" name="my_submit" value="Submit the form"/>
            </form>
        </body>
    </html>
"""

# 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)

这篇关于表格表格作为字典输入python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆