我尝试运行以下代码,但在显示结果时遇到问题。 另外,我使用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
如果您不想在Jupyter Notebook中工作,则可以data
另存为HTML文件并在浏览器中打开它。
with open("data.html", "w") as file:
file.write(data)
您只能在浏览器中呈现 HTML,而不能在 Python 控制台/编辑器环境中呈现 HTML。
因此,它可以在Jupiter笔记本,Jupyter Lab等中使用。
充其量你调用 .data 来查看 HTML,但它也不会呈现。
我通过在 Jupiter Notebook 上运行代码解决了我的问题。
您可以在data.show_batch()
之后添加此代码:
plt.show()
除了编写文件之外,另一种选择是使用 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
只需使用 HTML 对象的数据组件。
with open("data.html", "w") as file:
file.write(data.data)