在 Python 中将 CSV 转换为 HTML 表格 [英] Converting CSV to HTML Table in Python

查看:71
本文介绍了在 Python 中将 CSV 转换为 HTML 表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 .csv 文件中获取数据并导入到 Python 中的 HTML 表中.

这是 csv 文件 https://www.mediafire.com/?mootyaa33bmijiq

上下文:
csv 填充了来自足球队的数据 [年龄组、回合、反对派、球队得分、反对派得分、位置].我需要能够选择一个特定的年龄组,并且只在单独的表格中显示这些详细信息.

到目前为止,这就是我所拥有的......

infile = open("Crushers.csv","r")对于 infile 中的行:row = line.split(",")年龄 = 行 [0]周 = 行 [1]opp = 行[2]ACscr = 行[3]OPPscr = 行 [4]位置 = 行 [5]如果年龄=='U12':打印(周,opp,ACscr,OPPscr,位置)

解决方案

在开始打印所需的行之前,输出一些 HTML 以设置适当的表结构.

当您找到要打印的行时,以 HTML 表格行格式输出.

#开始表格打印(<表>")# 列标题打印(")打印(周")打印(<td>Opp</td>")打印(<td>ACscr</td>")打印(<td>OPPscr</td>")打印(<td>位置</td>")打印(</th>")infile = open("Crushers.csv","r")对于 infile 中的行:row = line.split(",")年龄 = 行 [0]周 = 行 [1]opp = 行[2]ACscr = 行[3]OPPscr = 行 [4]位置 = 行 [5]如果年龄=='U12':打印(")打印(<td>%s</td>"%周)打印(<td>%s</td>"% opp)打印(<td>%s</td>"% ACscr)打印(<td>%s</td>"% OPPscr)打印(<td>%s</td>"%位置)打印(</tr>")# 结束表打印(</table>")

I'm trying to take data from a .csv file and importing into a HTML table within python.

This is the csv file https://www.mediafire.com/?mootyaa33bmijiq

Context:
The csv is populated with data from a football team [Age group, Round, Opposition, Team Score, Opposition Score, Location]. I need to be able to select a specific age group and only display those details in separate tables.

This is all I've got so far....

infile = open("Crushers.csv","r")

for line in infile:
    row = line.split(",")
    age = row[0]
    week = row [1]
    opp = row[2]
    ACscr = row[3]
    OPPscr = row[4]
    location = row[5]

if age == 'U12':
   print(week, opp, ACscr, OPPscr, location)

解决方案

Before you begin printing the desired rows, output some HTML to set up an appropriate table structure.

When you find a row you want to print, output it in HTML table row format.

# begin the table
print("<table>")

# column headers
print("<th>")
print("<td>Week</td>")
print("<td>Opp</td>")
print("<td>ACscr</td>")
print("<td>OPPscr</td>")
print("<td>Location</td>")
print("</th>")

infile = open("Crushers.csv","r")

for line in infile:
    row = line.split(",")
    age = row[0]
    week = row [1]
    opp = row[2]
    ACscr = row[3]
    OPPscr = row[4]
    location = row[5]

    if age == 'U12':
        print("<tr>")
        print("<td>%s</td>" % week)
        print("<td>%s</td>" % opp)
        print("<td>%s</td>" % ACscr)
        print("<td>%s</td>" % OPPscr)
        print("<td>%s</td>" % location)
        print("</tr>")

# end the table
print("</table>")

这篇关于在 Python 中将 CSV 转换为 HTML 表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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