编辑 HTML,插入 CSS 引用 [英] Edit HTML, inserting CSS reference

查看:33
本文介绍了编辑 HTML,插入 CSS 引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以从 CSV 文件创建 HTML 页面,如下所示:

It's possible to create HTML page from a CSV file, with the following:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('../data.csv',delimiter=';', engine='python')
df.to_html('csv.html')

我想让 HTML 尊重 csv.css 中的一些 CSS.一种方法是手动编辑 csv.html head,插入:

I would like to make that HTML to respect some CSS present in csv.css. One way to get this is to manually edit csv.html head, inserting:

<head><link rel="stylesheet" type="text/css" href="csv.css"></head> 

如何通过编程(使用 Python)而不是手动完成?

Instead of doing it manually, how one can get there programmatically (using Python)?

推荐答案

to_html 方法不会输出整个 HTML 文档.相反,它只创建一个 table 元素.

The to_html method does not output an entire HTML document. Instead, it just creates a single table element.

如果你想包含 CSS,你必须创建额外的 HTML 元素,并在写入数据之前自己插入它们.一种最简单的方法如下:

If you want to include CSS, you have to create additional HTML elements, and insert them yourself before writing the data. One of the simplest ways is as follows:

with open('test.html', 'w') as fobj:
    fobj.write('<html><head><link rel="stylesheet" href="test.css"></head><body>')
    df.to_html(fobj)
    fobj.write('</body></html>')

to_html 的第一个参数必须是一个类似文件的对象:所以它可以是上面例子中的文件对象,也可以是 StringIO.

The first argument of to_html has to be a file-like object: so it can be either a file object as in the above example or a StringIO.

这篇关于编辑 HTML,插入 CSS 引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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