如何使用python向单词列表添加div

如何使用python向单词列表添加div,python,Python,我正在尝试用标准HTML创建一个表。但是我有很多单词需要包装在div标签中。通常我会做这个服务器端或其他什么的,但这在这个项目中不是现实。因此,我仅限于简单的旧web技术。不过,我想知道是否有一种python解决方案可以解决我的问题,即创建一个小脚本,以便我可以运行给定单词的列表,输出所需的HTML,然后将HTML复制并粘贴到各种HTML文件中。因此,如果我有下面的单词列表,我可以运行一个python程序,并为每个单词添加所需的div和类。一个问题是顺序需要增加到给定的字数 言语 输出应该是什么

我正在尝试用标准HTML创建一个表。但是我有很多单词需要包装在div标签中。通常我会做这个服务器端或其他什么的,但这在这个项目中不是现实。因此,我仅限于简单的旧web技术。不过,我想知道是否有一种python解决方案可以解决我的问题,即创建一个小脚本,以便我可以运行给定单词的列表,输出所需的HTML,然后将HTML复制并粘贴到各种HTML文件中。因此,如果我有下面的单词列表,我可以运行一个python程序,并为每个单词添加所需的div和类。一个问题是顺序需要增加到给定的字数

言语

输出应该是什么

 <div class="Rtable Rtable--1cols">
<div style="order:0" class="Rtable-cell-head">animals</div>
    <div style="order:1;" class="Rtable-cell">cat</div>
    <div style="order:2;" class="Rtable-cell">dog</div>
    <div style="order:3;" class="Rtable-cell">mouse</div>
    <div style="order:4;" class="Rtable-cell">lion</div>
</div>
jinja解决方案:

from __future__ import print_function
from jinja2 import Template

template = Template("""
<div class="Rtable Rtable--1cols">
<div style="order:0" class="Rtable-cell-head">animals</div>
    {%- for order, animal in animals %}
        <div style="order:{{ order }};" class="Rtable-cell">{{ animal }}</div>
    {%- endfor %}
</div>
""")

animals = """
cat
dog
mouse
lion
""".split()

print(template.render(animals=list(enumerate(animals, 1))))
此解决方案类似于下面的node.js解决方案

但是,如果您的用户懂javascript而不懂Python,那么node.js解决方案可能会更好:

const animals = `
  cat
  dog
  mouse
  lion
  `.split('\n').map(v => v.trim()).filter(v => !!v);

const animal_template = (animal, order) => `<div style="order:${order+1};" class="Rtable-cell">${animal}</div>`;

const template = animals => `
<div class="Rtable Rtable--1cols">
<div style="order:0" class="Rtable-cell-head">animals</div>
    ${animals.map(animal_template).join('\n    ')}
</div>`

console.log(template(animals));
jinja解决方案:

from __future__ import print_function
from jinja2 import Template

template = Template("""
<div class="Rtable Rtable--1cols">
<div style="order:0" class="Rtable-cell-head">animals</div>
    {%- for order, animal in animals %}
        <div style="order:{{ order }};" class="Rtable-cell">{{ animal }}</div>
    {%- endfor %}
</div>
""")

animals = """
cat
dog
mouse
lion
""".split()

print(template.render(animals=list(enumerate(animals, 1))))
此解决方案类似于下面的node.js解决方案

但是,如果您的用户懂javascript而不懂Python,那么node.js解决方案可能会更好:

const animals = `
  cat
  dog
  mouse
  lion
  `.split('\n').map(v => v.trim()).filter(v => !!v);

const animal_template = (animal, order) => `<div style="order:${order+1};" class="Rtable-cell">${animal}</div>`;

const template = animals => `
<div class="Rtable Rtable--1cols">
<div style="order:0" class="Rtable-cell-head">animals</div>
    ${animals.map(animal_template).join('\n    ')}
</div>`

console.log(template(animals));

您可以使用bs4创建、操作和删除html:pip安装beautifulsoup4

读取包含单词列表的文件,每行1个元素 创建所需的html:
您可以使用bs4创建、操作和删除html:pip安装beautifulsoup4

读取包含单词列表的文件,每行1个元素 创建所需的html:
请看一看或其他模板引擎。不幸的是,我不能使用模板引擎。我坚持使用前端web技术,但我认为我可以创建一个小脚本,在将输出的HTML添加到所需文件之前,我可以独立运行。您需要像BeautifulSoup这样的工具来解析HTML,因为您必须准备好各种习惯用法或错误。但是手工构建HTML是可以的,因为它只是一个微不足道的字符串处理。类似于wordlist中i的print prolog:printline_template.formati printepilog。我不明白。。。如果您可以使用python并安装一个额外的库,那么什么会阻止您使用模板库呢?在这种情况下,“前端web技术”是什么意思?我的网站上没有使用python。它仅限于HTML、css和js。这是因为我必须使用的电子商务平台,它们限制我只使用简单的HTML、css和js。。我认为一个可能的解决方案是创建一个脚本,我可以在我的机器上运行,将所需的HTML输出到一个文件中或仅在空闲的shell上,然后将此结果复制并粘贴到HTML文件中。这可能是一个愚蠢的想法,但鉴于局限性,这是我能想到的最好的方法。请看一看或其他模板引擎。不幸的是,我不能使用模板引擎。我坚持使用前端web技术,但我认为我可以创建一个小脚本,在将输出的HTML添加到所需文件之前,我可以独立运行。您需要像BeautifulSoup这样的工具来解析HTML,因为您必须准备好各种习惯用法或错误。但是手工构建HTML是可以的,因为它只是一个微不足道的字符串处理。类似于wordlist中i的print prolog:printline_template.formati printepilog。我不明白。。。如果您可以使用python并安装一个额外的库,那么什么会阻止您使用模板库呢?在这种情况下,“前端web技术”是什么意思?我的网站上没有使用python。它仅限于HTML、css和js。这是因为我必须使用的电子商务平台,它们限制我只使用简单的HTML、css和js。。我认为一个可能的解决方案是创建一个脚本,我可以在我的机器上运行,将所需的HTML输出到一个文件中或仅在空闲的shell上,然后将此结果复制并粘贴到HTML文件中。这也许是一个愚蠢的想法,但考虑到我的局限性,这是我能想到的最好的办法。我喜欢诺德,但我会看看他们怎么说。你的纯解决方案注意到了一件事,如果有空格,比如说,如果我将“白犀牛”添加到动物列表中,这会被分成两个不同的分区。我已经用Python版本的node.js动物拆分代码更新了我的答案。太棒了,我自己也喜欢nodejs,但我会看看他们怎么说。您的纯解决方案注意到一件事,如果有空格,例如,如果我将“white rhino”添加到动物列表中,这将被分成两个不同的div。我已使用node.js动物拆分代码的Python版本更新了我的答案。
animals = [animal.strip() for animal in """
cat
dog
mouse
lion
""".splitlines() if animal.strip()]
const animals = `
  cat
  dog
  mouse
  lion
  `.split('\n').map(v => v.trim()).filter(v => !!v);

const animal_template = (animal, order) => `<div style="order:${order+1};" class="Rtable-cell">${animal}</div>`;

const template = animals => `
<div class="Rtable Rtable--1cols">
<div style="order:0" class="Rtable-cell-head">animals</div>
    ${animals.map(animal_template).join('\n    ')}
</div>`

console.log(template(animals));
with open('/path/to/list', 'r') as f:
    lines = f.readlines()
from bs4 import BeautifulSoup

# creating parent
soup = BeautifulSoup('<div class="Rtable Rtable--1cols"></div>')

# adding head
new_tag = soup.new_tag('div', **{'class': 'Rtable Rtable--1cols'}) 
new_tag.string = line[0]
soup.append(new_tag)

# adding elements
for i, line in enumerate(lines[1:]):
    new_tag = soup.new_tag('div', **{'class': 'Rtable-cell', 'style': f'order:{i};'}) 
    new_tag.string = line
    soup.append(new_tag)

print(soup)