Convert text file to html? I have a script that converts a text file to html. The text file is an output of a program (needle). The function takes the text file as an input. I am aware the script is awful. I use this function in Django. I would like to improve it. Can you help me with it?. Here is the result file from the script.

def write_html(result):
    sequence_1 = ''
    sequence_2 = ''
    identity = ''
    similarity = ''
    gaps = ''
    html_string = ''
    with open("/Users/catuf/Desktop/needle_align.txt", "w") as e:
        e.write('{% extends "database/base_table.html" %}' + "\n")
        e.write('\n')
        e.write("{% block content %}"+"\n")
        table1 = '''
        &lt;!DOCTYPE html&gt;
        &lt;html&gt;
        &lt;head&gt;
        &lt;style&gt;
        table, th, td {
          border: 1px solid black;
        }
        &lt;/style&gt;
        &lt;/head&gt;
        &lt;body&gt;

        &lt;table&gt;
          &lt;tr&gt;
            &lt;th&gt;Query Sequence &lt;/th&gt;
            &lt;th&gt;Matched Sequence &lt;/th&gt;
            &lt;th&gt;Identity &lt;/th&gt;
            &lt;th&gt;Similarity &lt;/th&gt;
            &lt;th&gt;Gaps &lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
        '''

        table2 = """
          &lt;/tr&gt;
        &lt;/table&gt;

        &lt;/body&gt;
        &lt;/html&gt;
        """
        for line in result.splitlines():
            #s = html.unescape(lines)
            #print(line)
            if line.startswith('# 1:'):
                line = line.split()
                sequence_1 = line[2]
            elif line.startswith('# 2:'):
                line = line.split()
                #print(line)
                sequence_2 = line[2]

            elif line.startswith('# Identity:'):
                identity = re.search(r"\d{1,3}\.\d*\%", line)
                identity = identity.group()
                identity = identity.replace('%', '')
                #identity = float(identity)* 100
                #identity = str(identity)

            elif line.startswith('# Similarity:'):
                similarity = re.search(r"\d{1,3}\.\d*\%", line)
                similarity = similarity.group()
                similarity = similarity.replace('%', '')

            elif line.startswith('# Gaps:'):
                line = line.split()
                gaps = line[2]

            elif line.startswith('#'):
                pass
            else:
                s = "&lt;pre&gt;" + line + "&lt;/pre&gt;"
                html_string += s

        e.write(table1)
        e.write("&lt;td&gt;" + sequence_1 + '&lt;/td&gt;' + '\n')
        e.write("&lt;td&gt;" + sequence_2 + '&lt;/td&gt;'+ '\n')
        e.write("&lt;td&gt;"+ identity + "%" + '&lt;/td&gt;'+ '\n')
        e.write("&lt;td&gt;"+ similarity+ "%" +'&lt;/td&gt;'+ '\n')
        e.write("&lt;td&gt;"+ gaps+ '&lt;/td&gt;'+ '\n')
        e.write(table2)
        e.write(html_string)
        e.write("&lt;/head&gt;" + "\n")
        e.write("{% endblock content %}"+"\n")
Functions

Right now, write_html manages both parsing and writing to a file. Separate this function into two functions, perhaps parse_data and write_data.

String Formatting

This

s = "&lt;pre&gt;" + line + "&lt;/pre&gt;"
e.write("&lt;td&gt;" + sequence_1 + '&lt;/td&gt;' + '\n')


can be written like this

s = f"&lt;pre&gt;{line}&gt;&lt;/pre&gt;"
e.write(f"&lt;td&gt;{sequence_1}&lt;/td&gt;\n")


The f"" allows you to directly implement variables into your strings.

Simplification

This

elif line.startswith('# Gaps:'):
    line = line.split()
    gaps = line[2]


can be written like this

elif line.startswith('# Gaps:'):
    gaps = line.split()[2]


The same with these two:

if line.startswith('# 1:'):
    sequence_1 = line.split()[2]
elif line.startswith('# 2:'):
    sequence_2 = line.split()[2]


Variable Assignments

This

sequence_1 = ''
sequence_2 = ''
identity = ''
similarity = ''
gaps = ''
html_string = ''


can be written like this

sequence_1, sequence_2, identity, gaps, similarity, html_string = '', '', '', '', '', ''


Type Hints

You can use type hints to make it clear what is being accepted as parameters, and what is being returned by the function.

From this

def write_html(result):


to this

def write_html(result: str) -&gt; None:


Docstrings

You should include a docstring at the beginning of every function, method, class, and module you write. This will allow documentation to identify what these are supposed to do.

def write_html(result: str) -&gt; None:
    """
    (Description about this method here)
    """
    ... code here ...