简体   繁体   English

使用python将csv文件转换为html中的可编辑表

[英]csv file to editable table in html using python

I have a csv file.我有一个 csv 文件。 I have converted into html file.我已经转换成html文件。 how to make the html table editable from python如何使 html 表可从 python 编辑

import pandas as pd
df=pd.read_csv("D:/valli/abc.csv")
df.to_html("x.html")

I do not know how to proceed with flask, javascript and jquery我不知道如何继续使用 flask、javascript 和 jquery

Here's something that might work out for you.这可能对您有用。 I don't know HTML that well, so please let me know if I'm not understanding the correct structure of a table.我不太了解 HTML,所以如果我不理解表格的正确结构,请告诉我。

I'm using a sample dataframe with the following values:我正在使用具有以下值的示例数据框:

  Id Value   Name
0  1     2   Paul
1  2     5    Sam
2  3     7   Jill
3  4    10   Karl
4  5    15  Sally
5  6     4   Irma

To accomplish your goal, we're going to use Python's join() function.为了实现您的目标,我们将使用 Python 的 join() 函数。 Here's the script:这是脚本:

import pandas as pd

ddict = {
    'Id': ['1', '2', '3', '4', '5', '6'],
    'Value': ['2','5', '7', '10', '15', '4'],
    'Name': ['Paul','Sam', 'Jill', 'Karl', 'Sally', 'Irma']
}

df = pd.DataFrame(ddict)

### Print the table tag
print('<table>')

### Print the headers to a <th> tag; Close each <th> for every line
print('\t<tr>\n\t\t<th>' + '\t\t<th>'.join([f'{i}</th>\n' for i in df.columns.values.tolist()]) + '\t</tr>')

### Iterate over dataframe values; Enclose each value in a <td> tag
for i, v in enumerate(df.values):
    print('\t<tr>\n\t\t<td>' + '\t\t<td>'.join([f'{x}</td>\n' for x in v]) + '\t</tr>')

### Print table tag closure.
print('</table>')

Output:输出:

<table>
    <tr>
        <th>Id</th>
        <th>Value</th>
        <th>Name</th>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>Paul</td>
    </tr>
    <tr>
        <td>2</td>
        <td>5</td>
        <td>Sam</td>
    </tr>
    <tr>
        <td>3</td>
        <td>7</td>
        <td>Jill</td>
    </tr>
    <tr>
        <td>4</td>
        <td>10</td>
        <td>Karl</td>
    </tr>
    <tr>
        <td>5</td>
        <td>15</td>
        <td>Sally</td>
    </tr>
    <tr>
        <td>6</td>
        <td>4</td>
        <td>Irma</td>
    </tr>
</table>

Tested and works测试和工作

reference from the ans below参考下面的答案

Render an editable table using Flask, Jinja2 templates, then process the form data returned 使用 Flask、Jinja2 模板渲染一个可编辑的表格,然后处理返回的表格数据

you can do this by using FormField to generate an editable table from your database either you can use database or you can use CSV below example shows how to generate an editable table from csv您可以通过使用 FormField 从数据库生成可编辑表格来执行此操作 您可以使用数据库,也可以使用 CSV 下面的示例显示了如何从 csv 生成可编辑表格

form.py表单.py

class MemberForm(Form):
    name = StringField('name')
    member_id = StringField('member_id')
    inbox_share = IntegerField('inbox_share')
    # etc.

class TeamForm(Form):
    title = StringField('title')
    teammembers = FieldList(FormField(MemberForm))

views.py视图.py

@app.route('/support/team-members-update', methods=['GET','POST'])
def update_team_member():
    #below is to cover csv col to list
    columns = defaultdict(list)
    with open('file.csv') as cavfile:
         reader = csv.DictReader(csvfile)
         for row in render:
             for (k,v) in row.items():
                  columns[k].append(v)
    member_id= columns['name of your col 1'] #this will generate a list of your col
    inbox_share = columns['name of you col 2'] #this will generate list of you col

    for i,j in zip(list_col1, list_col2):
         member_form = MemberForm()
         member_form.member_id = i
         member_form.inbox_share = j
         teamform.teammembers.append_entry(member_form) 
    return render_template('ediit-team.html',teamform=teamform)

edit-team.html编辑团队.html

<html>
<head>
    <title>Edit Team Members</title>
</head>
<body>
    <h1>Edit Team</h1>
    <div>
        <form action="" method="post" name="teamform">
            {{ teamform.hidden_tag() }}
            Team Title: {{ teamform.title }}<br>
            <div>
                <table>
                    <tr>
                        <th> Name </th>
                        <th> ID </th>
                        <th> Inbox Share </th>
                    </tr>
                    {% for member in teamform.teammembers %}
                    <tr>                            
                        <td>{{ member.member_id }}</td>
                        <td>{{ member.inbox_share }}</td>
                    </tr>
                    {% endfor %}
                </table>
            </div>
            <p><input type="submit" name="edit" value="Send"></p>
        </form>
    </div>
</body>

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

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