如何用html编写csv文件?

时间:2019-06-27 08:28:28

标签: python html django csv

我已经读取了csv文件,但是遇到一个问题,即如何读取CSV文件并将其保存在 table.html 中?

import csv

html_about = ''
names = []

with open('filo.csv') as data_file:
    csv_data = csv.reader(data_file)

    for line in csv_data:
    names.append(f'{line[0]}')

html_output = '\n<ul>'

for name in names:
    html_output += f'\n\t<li>{name}</li>'

html_output += '\n</ul>'

from prettytable import PrettyTable
x = PrettyTable(line[0])
html_code = x.get_html_string()
html_file = open('table.html','w')
html_file = html_file.write(html_code)

1 个答案:

答案 0 :(得分:1)

我建议您使用熊猫库,

它有pd.read_csv,还有pd.to_html

用法应如下所示,让我知道这是否对您有用:

import pandas as pd

df = pd.read_csv('filo.csv')

with open('table.html', 'w') as html_file:
    df.to_html(html_file)