如何在Python字符串中插入带有标记的列表

如何在Python字符串中插入带有标记的列表,python,Python,我正在编写这个ppython片段。如何在初始化文件时在write()中发布HTML列表以及硬编码标记 基本上我想写第三个文件 <li>apple</li> <li>banana</li> <li>cherry</li> 苹果公司 香蕉 樱桃 这是密码 f = open("D:/App/Test.html", "a") f.write( """&l

我正在编写这个ppython片段。如何在初始化文件时在
write()
中发布HTML列表以及硬编码标记

基本上我想写第三个文件

<li>apple</li> 
<li>banana</li> 
<li>cherry</li> 
苹果公司
  • 香蕉
  • 樱桃 这是密码

    f = open("D:/App/Test.html", "a")
    f.write(
    """<!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Test</title>
      </head>
      <body>
      <div class="container py-4">
      <ul>
      """
        fruits = ["apple", "banana", "cherry"]
            for x in fruits:
             print(x)
    
        """
        </ul></div>
          </body>
        </html>"""
    )
    f.close()
    
    f=open(“D:/App/Test.html”,“a”)
    f、 写(
    """
    试验
    
      """ 水果=[“苹果”、“香蕉”、“樱桃”] 对于水果中的x: 打印(x) """
    """ ) f、 关闭()
    一种方法是创建模板。注
    {items}
    是要插入的位置:

    template=''\
    试验
    
      {items}
    '''
    然后构建插入字符串:

    水果=[“苹果”、“香蕉”、“樱桃”]
    items='\n'.join([f'
  • {x}
  • '表示水果中的x]) 将open('test.html','w')作为f: f、 写入(template.format(items=items))
    输出:

    
    试验
    
    • 苹果
    • 香蕉
    • 樱桃

    一种方法是创建模板。注
    {items}
    是要插入的位置:

    template=''\
    试验
    
      {items}
    '''
    然后构建插入字符串:

    水果=[“苹果”、“香蕉”、“樱桃”]
    items='\n'.join([f'
  • {x}
  • '表示水果中的x]) 将open('test.html','w')作为f: f、 写入(template.format(items=items))
    输出:

    
    试验
    
    • 苹果
    • 香蕉
    • 樱桃

    我删除了注释过的代码,以便于阅读

    你可以用下面的方法来做

    f = open("D:/App/Test.html", "a")
    fruits = ["apple", "banana", "cherry"]
    
    list_html= ''.join([f"<li>{i}</li>\n" for i in fruits])
    
    f.write(list_html)
    f.close()
    
    f=open(“D:/App/Test.html”,“a”)
    水果=[“苹果”、“香蕉”、“樱桃”]
    list_html=''.join([f”
  • {i}
  • \n“表示水果中的i]) f、 写入(列表\ html) f、 关闭()

    .join
    将列表转换为字符串,而
    f“
  • {i}
  • \n”for i in fruits
    将遍历循环中的所有元素,并使它们成为列表格式

    我删除注释过的代码以使其更易于阅读

    你可以用下面的方法来做

    f = open("D:/App/Test.html", "a")
    fruits = ["apple", "banana", "cherry"]
    
    list_html= ''.join([f"<li>{i}</li>\n" for i in fruits])
    
    f.write(list_html)
    f.close()
    
    f=open(“D:/App/Test.html”,“a”)
    水果=[“苹果”、“香蕉”、“樱桃”]
    list_html=''.join([f”
  • {i}
  • \n“表示水果中的i]) f、 写入(列表\ html) f、 关闭()

    .join
    将列表转换为字符串,而
    f“
  • {i}
  • \n”for i in fruits
    将遍历循环中的所有元素,并使它们以列表格式出现。这会将字符串列表放入html_代码中。以你的逻辑为基础,这样你就能明白发生了什么

    fruits = ["apple", "banana", "cherry"]
        
    f = ["  <li>{0}</li>".format(i) for i in fruits]
    
    html_list = ""
    for x in f:
        html_list += x + "\n  "
        
    html_code = """<!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Test</title>
      </head>
      <body>
      <div class="container py-4">
      <ul>
      """ + html_list + """
        </ul></div>
          </body>
        </html>"""
    
    
    
    for i in html_code.split("\n"):
        print(i)
    
    水果=[“苹果”、“香蕉”、“樱桃”]
    f=[“
  • {0}
  • ”。水果中i的格式(i) html_list=“” 对于f中的x: html\u列表+=x+“\n” html_code=“” 试验
      “+html\u列表+”
    """ 对于html_代码中的i.split(“\n”): 印刷品(一)
    试试这个。这会将字符串列表放入html_代码中。以你的逻辑为基础,这样你就能明白发生了什么

    fruits = ["apple", "banana", "cherry"]
        
    f = ["  <li>{0}</li>".format(i) for i in fruits]
    
    html_list = ""
    for x in f:
        html_list += x + "\n  "
        
    html_code = """<!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Test</title>
      </head>
      <body>
      <div class="container py-4">
      <ul>
      """ + html_list + """
        </ul></div>
          </body>
        </html>"""
    
    
    
    for i in html_code.split("\n"):
        print(i)
    
    水果=[“苹果”、“香蕉”、“樱桃”]
    f=[“
  • {0}
  • ”。水果中i的格式(i) html_list=“” 对于f中的x: html\u列表+=x+“\n” html_code=“” 试验
      “+html\u列表+”
    """ 对于html_代码中的i.split(“\n”): 印刷品(一)
    谢谢标记,但在
    项='\n'上似乎有语法错误。请连接([f'
  • {x}
  • 'for x in fruits])
    行。我想这取决于python版本?应在python3中运行;在python2中可能会失败(可能会导入fstring包?)。在python2中,您应该可以使用
    “\n”来选择它。join([“
  • {x}
  • ”.format(x=x)for x in fruits])
    @Behseini F-string格式语法(
    F'.
    )是在Python3.6中引入的。谢谢标记,但它看起来在
    项='\n>上有语法错误。join([f'
  • {x}
  • '对于水果中的x])
    行。我想这取决于python版本?应该在python3中运行;可能在python2中失败(可能导入fstring包?)。在python2中,您应该可以使用
    “\n”来选择它。join([“
  • {x}
  • ”.format(x=x)对于水果中的x])
    @Behseini f-string格式语法(
    f'.
    )是在Python 3.6中引入的。