使用Python将HTML标签添加到文本

时间:2019-03-20 10:47:12

标签: python text

我在文件“ 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)

1 个答案:

答案 0 :(得分:0)

下面的代码满足了我的需要。

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)