使用python将序列导入HTML格式

时间:2017-02-16 12:27:52

标签: python html spacing

我尝试使用以下方法从文本文件导入1到999个字母之间的序列:

input_file.open(textfile.txt)
sequence = input_file.read()
inputfile_close()

然后我打开一个新的HTML文件来写入:

output_file.open(new.html, 'w')

然后我使用以下命令将HTML格式写入新文件:

output_file.write(-formatting-)

我目前正在使用:

    if character == 'A' or character == 'G' or character == 'I' or character == 'L' or character == 'P' or character == 'V' :
        output_file.write ('<font style="background-color:white;">' + character + '</font>')    
    if character == 'F' or character == 'Y' or character == 'W' :
        output_file.write ('<font style="background-color:green;">' + character + '</font>') # now we put the text we've read from the text file in
    if character == 'D' or character == 'E' :
        output_file.write ('<font style="background-color:orange;">' + character + '</font>') # now we put the text we've read from the text file in
    if character == 'H' or character == 'K' or character == 'R' :
        output_file.write ('<font style="background-color:red;">' + character + '</font>') # now we put the text we've read from the text file in
    if character == 'S' or character == 'T' :
        output_file.write ('<font style="background-color:purple;">' + character + '</font>') # now we put the text we've read from the text file in
    if character == 'C' or character== 'M' :
        output_file.write ('<font style="background-color:yellow;">' + character + '</font>') # now we put the text we've read from the text file in
    if character == 'N' or character == 'Q' :
        output_file.write ('<font style="background-color:blue;">' + character + '</font>') # now we put the text we've read from the text file in

为每个字母的颜色编码。

我的问题是我需要序列在每第10个字母后面有一个空格,并且在每第60个字母后面都有一个新行。 序列和编号都需要是等宽的。

任何建议都将不胜感激。

如果需要进一步的信息,请询问。

我用编码here制作了一张照片。 附加的编码是我目前正在使用的。

谢谢

1 个答案:

答案 0 :(得分:1)

我猜你读了sequence并在for循环中写了html?首先将格式化的序列写入字符串,然后将整个字符串写入html。

使用模运算符%,您可以添加空格/换行符。

formatted_seq = ""
count = 0
for character in sequence:
    if character in 'AGILPV':
        bg = "white"
    elif character in 'FYW':
        bg = "green"
    elif character in 'DE':
        bg = "orange"
    elif character in 'HKB':
        bg = "red"
    elif character in 'ST':
        bg = "purple"
    elif character in 'CM':
        bg = "yellow"
    elif character in 'NQ':
        bg = "blue"

    formatted_seq += '<font style="background-color:' + bg + ';">' + character + '</font>'

    count += 1
    if count%60 == 0:
        formatted_seq += '<br>'
    elif count%10 == 0:
        formatted_seq += '&nbsp;'