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 = '''
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th>Query Sequence </th>
<th>Matched Sequence </th>
<th>Identity </th>
<th>Similarity </th>
<th>Gaps </th>
</tr>
<tr>
'''
table2 = """
</tr>
</table>
</body>
</html>
"""
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 = "<pre>" + line + "</pre>"
html_string += s
e.write(table1)
e.write("<td>" + sequence_1 + '</td>' + '\n')
e.write("<td>" + sequence_2 + '</td>'+ '\n')
e.write("<td>"+ identity + "%" + '</td>'+ '\n')
e.write("<td>"+ similarity+ "%" +'</td>'+ '\n')
e.write("<td>"+ gaps+ '</td>'+ '\n')
e.write(table2)
e.write(html_string)
e.write("</head>" + "\n")
e.write("{% endblock content %}"+"\n")