【问题标题】:Add HTML tags to text using Python使用 Python 将 HTML 标记添加到文本中
【发布时间】:2019-03-20 10:47:11
【问题描述】:

我在文件“File1”中有一个文本,其中包含以下文本:

-Accounting  
-HR

Some text  

-IT  
--Networks   
--Storage  
--DBA  

我需要一段代码,它会逐行读取 File1 并将“-”和“--”替换为适当的 HTML 标记,并将下面显示的最终结果保存在文本文件 File2 中

<ul>
<li>Accounting</li>
<li>HR</li>
</ul>

Some text

<ul><li>IT
       <ul>
          <li>Networks</li>
          <li>Storage</li>
          <li>DBA</li>
         </ul>
         </li></ul>

到目前为止,我尝试了以下代码。

我将用于检查当前行是否包含“-”或“--”的两个布尔值最初设置为 False。如果当前行中有“-”或“--”,则代码在行首添加适当的标签,将布尔值更改为 True 并转到下一行。

现在布尔值用于查看上一行中是否有“-”或“--”,如果有破折号,它将在应该在上一行的行的开头添加适当的标签,但我们'已经在下一行了。另一种方法是检查下一行是否以“-”或“--”开头,但我不知道该怎么做。当我使用 next() 时,该行被跳过。同时从两个文件中读取一个在前一行并检查它在下一行中的内容会是一个更好的解决方案吗?

    single_dash_prev_line = False
    double_dash_prev_line = False
    for line in File1:
        current_line = line

        if line[0] == "-":
            if line[1] != "-":
                if single_dash_prev_line == False:
                    new_line = "<ul><li>" + current_line[1:]
                    File2.write(new_line)
                    single_dash_prev_line = True
                elif single_dash_prev_line == True:
                    new_line = "</li><li>" + current_line[1:]
                    File2.write(new_line)
                    single_dash_prev_line = True


            elif line[1] == "-":
                if single_dash_prev_line == True:
                    new_line = "<ul><li>" + line[2:]
                    print(new_line)
                    File2.write(new_line)
                    double_dash_prev_line = True
                elif double_dash_prev_line == True:
                    new_line = "</li><li>" + line[2:]
                    File2.write(new_line)
                    double_dash_prev_line = True

        elif single_dash_prev_line == True:
            new_line = "</li></ul>" + current_line[1:]
            File2.write(new_line)
            single_dash_prev_line = False
        elif double_dash_prev_line == True:
            new_line = "</li></ul>" + current_line[1:]
            File2.write(new_line)
            single_dash_prev_line = False

        else:
            single_dash_prev_line = False
            double_dash_prev_line = False
            File2.write(current_line)

【问题讨论】:

  • 只需学习如何将内容写入文件,然后将内容写入名为index.html 的文件。您可以将 HTML 标记写入该文件:)
  • Seb_ 你在解决这个问题时做了什么尝试?我们不想帮助你成为更好的程序员。所以请告诉我们你尝试了什么。假设你不知道,我会给你一个提示:遍历文本文件中的 de 行,正则表达式“-”并替换为占位符。
  • @duhaime 抱歉应该更清楚。我知道如何写入文件。我知道如何添加标签。但是我有数千个这样的文本文件,我必须添加 HTML 标记,所以没有手动添加的选项,如果它已经是几页的话就已经完成了。
  • @Seb_ 只是读取您的每个文件并生成一个新的相应 html 文件——不要将 html 写入输入文件,使用您的标记生成新的输出文件...
  • @duhaime 我想我又不清楚了。我在这里需要的是根据当前行和上一行中的内容添加适当标签的逻辑。或者可以重写它以检查当前行和下一行,但我不确定这个选项,因为正如我所说的 next() 转到下一行。或者也许从两个文件中读取是一种选择

标签: python text


【解决方案1】:

下面的代码做了我需要的。

with open("finalfile.txt", 'w', encoding='utf-8') as File2, open("test.txt", "r", encoding='utf-8') as File1:
previous_line = ""
new_line = ""
double_dash_prev_line = False
single_dash_prev_line = False
for line in File1:
    current_line = line
    if line[0] == "-":
        if line[1] != "-":
            if single_dash_prev_line == False and double_dash_prev_line == False:
                new_line = "<ul><li> " + current_line[1:]
                File2.write(new_line)
                single_dash_prev_line = True
                double_dash_prev_line = False
            elif single_dash_prev_line == True:
                new_line = "</li><li> " + current_line[1:]
                File2.write(new_line)
                single_dash_prev_line = True
                double_dash_prev_line = False
            elif double_dash_prev_line == True:
                new_line = "</ul></li></ul><ul><li> " + current_line[1:]
                File2.write(new_line)
                single_dash_prev_line = True
                double_dash_prev_line = False


        elif line[1] == "-":
            if single_dash_prev_line == True:
                new_line = "<ul><li> " + line[2:]
                File2.write(new_line)
                double_dash_prev_line = True
                single_dash_prev_line = False
            elif double_dash_prev_line_line == True:
                new_line = "</li><li> " + line[2:]
                File2.write(new_line)
                double_dash_prev_line = True
                single_dash_prev_line = False
    elif single_dash_prev_line == True:
        new_line = "</li></ul> " + current_line[1:]
        File2.write(new_line)
        single_dash_prev_line = False
        double_dash_prev_line = False
    elif double_dash_prev_line_line == True:
        new_line = "</li></ul></ul> " + current_line[1:]
        File2.write(new_line)
        double_dash_prev_line = False
        single_dash_prev_line = False
    else:
        single_dash_prev_line = False
        double_dash_prev_line = False
        File2.write(current_line)

【讨论】:

    猜你喜欢
    • 2021-08-15
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    相关资源
    最近更新 更多