【问题标题】:How to generate my python output as a HTML如何将我的 python 输出生成为 HTML
【发布时间】:2020-03-31 09:51:47
【问题描述】:
import dominate
from dominate.tags import *
doc = dominate.document(title='Cell Value report')
with doc:
    with div():
        attr(cls='body')
        h2('Values Missing in the files.....')
    with div(id='header').add(ol()):
        for i in unique_file:
            li(i.title())

我试过这个,以 HTML 格式生成我的 python 输出。
如果我在os.listdir 中硬编码路径,HTML 部分工作正常
但如果我使用路径作为输入,它会显示错误。

search_path = input("Enter directory path to search: ")#directory path
for fname in os.listdir(path=search_path):

这是错误
TypeError: listdir: path 应该是 string, bytes, os.PathLike or None, not input_

我什至尝试了一个库 yattag
我在 python 中有一个 List[],我必须循环并打印为 HTML 中的列表。
我在 yattag 中尝试过,但我无法实现,我不确定我是什么做错了。
我应该使用任何其他库来实现我的输出。
请给我一些建议。

【问题讨论】:

  • 看起来您的通配符导入 (from dominate.tags import *) 隐藏了内置的 input 函数(HTML 中有一个“输入”标签...)。这就是为什么您应该从不使用通配符导入,无论您的 lib 文档说什么。
  • @brunodesthuilliers 还有其他我应该寻找的方法吗?
  • "我还有其他方法吗" => 将 from dominator.tags import * 替换为 from dominator import tags,然后将所有对 div()h2()li() 等的调用替换为 (分别)tags.div()tags.h2() 等。
  • 或明确导入您要使用的标签(from dominator.tags import div, li, h2 等),但第一个解决方案避免了每次您想使用另一个标签时都必须编辑导入,并使代码更清晰(您无需检查导入即可知道 divli 或其他任何来源)并且更强大(您没有此类名称冲突...)。
  • 注意:请记住(至少在现实生活中)您阅读代码的次数比编写代码的次数要多得多,因此从长远来看,多输入几个字符确实值得付出努力;-)

标签: python html dominate yattag


【解决方案1】:

错误是由于通配符导入造成的。from dominate.tags import *dominate.tags 定义了一个 input 类,它隐藏了内置的 input() 函数。

此代码运行良好,没有错误。

from dominate import tags
with doc:
    with tags.div():
        tags.attr(cls='body')
        tags.h2('Values Missing in the files.....')
    with tags.div(id='header').add(tags.ol()):
        for i in unique_file:
            tags.li(i.title())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多