6

我尝试运行以下代码,但在显示结果时遇到问题。另外,我使用 pycharm IDE。

from fastai.text import *

data = pd.read_csv("data_elonmusk.csv",  encoding='latin1')
data.head()

data = (TextList.from_df(data, cols='Tweet')
                .split_by_rand_pct(0.1)
                .label_for_lm()
                .databunch(bs=48))

data.show_batch()

我运行“data.show_batch()”行时的输出是:

IPython.core.display.HTML object
4

6 回答 6

3

If you don't want to work within a Jupyter Notebook you can save data as an HTML file and open it in a browser.

with open("data.html", "w") as file:
    file.write(data)
于 2020-09-09T14:11:20.470 回答
2

我通过在 Jupiter Notebook 上运行代码解决了我的问题。

于 2019-12-06T12:12:48.337 回答
2

您可以在之后添加此代码data.show_batch()

plt.show()

于 2020-05-15T04:01:48.900 回答
2

您只能在浏览器中呈现 HTML,而不能在 Python 控制台/编辑器环境中呈现。

因此它适用于 Jupiter notebook、Jupyter Lab 等。

充其量你调用 .data 来查看 HTML,但它也不会呈现

于 2020-08-19T13:36:28.990 回答
1

只需使用 HTML 对象的数据组件。

with open("data.html", "w") as file:
    file.write(data.data)
于 2021-07-07T12:17:17.367 回答
1

除了编写文件之外,另一个选择是使用 Python 中的 HTML 解析器以编程方式编辑 HTML。Python中最常用的工具是beautifulsoup。你可以通过安装它

pip install beautifulsoup4

然后在你的程序中你可以做

from bs4 import BeautifulSoup

html_string = data.show_batch().data
soup = BeautifulSoup(html_string)
# do some manipulation to the parsed HTML object
# then do whatever else you want with the object
于 2021-05-04T00:43:07.233 回答