使用python将序列导入HTML格式

使用python将序列导入HTML格式,python,html,spacing,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 =

我正在尝试使用以下命令从文本文件导入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
如果字符='A'或字符='G'或字符='I'或字符='L'或字符='P'或字符='V':
output_file.write(“”+字符+“”)
如果字符='F'或字符='Y'或字符='W':
output_file.write(“”+字符+“”)#现在我们将从文本文件中读取的文本放入
如果字符='D'或字符='E':
output_file.write(“”+字符+“”)#现在我们将从文本文件中读取的文本放入
如果字符='H'或字符='K'或字符='R':
output_file.write(“”+字符+“”)#现在我们将从文本文件中读取的文本放入
如果字符='S'或字符='T':
output_file.write(“”+字符+“”)#现在我们将从文本文件中读取的文本放入
如果字符='C'或字符='M':
output_file.write(“”+字符+“”)#现在我们将从文本文件中读取的文本放入
如果字符='N'或字符='Q':
output_file.write(“”+字符+“”)#现在我们将从文本文件中读取的文本放入
对每个字母进行颜色编码

我的问题是,我需要序列在每10个字母后有一个空格,在每60个字母后有一个新行。 序列和编号都需要单间隔

如有任何建议,将不胜感激

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

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


谢谢你

我想你读了
序列
并在
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;'
formatted_seq=“”
计数=0
对于顺序中的字符:
如果“AGILPV”中的字符:
bg=“白色”
“FYW”中的elif字符:
bg=“绿色”
“DE”中的elif字符:
bg=“橙色”
“HKB”中的elif字符:
bg=“红色”
“ST”中的elif字符:
bg=“紫色”
“CM”中的elif字符:
bg=“黄色”
“NQ”中的elif字符:
bg=“蓝色”
格式化的_seq+=''+字符+''
计数+=1
如果计数%60==0:
格式化的_seq+='
' elif计数%10==0: 格式化的_seq+=''
Pro减少长
if
案例的提示:
if字符('A','G','I','L','P','V'):
使其更易于阅读。我也会考虑把所有修改后的字符放在一个列表中,并提取像S:<代码>格式化的字符[0:10] + ''/Cord]之类的块,只为第60个字母做一个普通计数器并插入你的断线。@ Trink可以给我一个计数器的例子吗?为什么将它们放在一个列表中然后提取它们会有好处呢?Jacob,你可以不通过
output\u file.write(…)
直接写入文件,而是将结果存储在
result=''.join(格式化的\u字符)
中,然后对范围(0,len(result),60)中的i执行
操作:output\u file.write(result[i:i+60]+“
”)
或类似的东西。@是否已扭曲,以便我导入的序列将是格式化字符?或者它们是一个单独的实体?
我需要序列在每10个字母后有一个空格,在每60个字母后有一个新行。
-这部分问题几天前没有解决吗?我尝试过使用它,但我不确定我是否正确。我试着用这个部分来代替我的版本:对于字符in(序列):如果字符in('A','G','I','L','P','V'):output_file.write(''+character+'')是它的正确位置吗?@JacobSørensen是的。因此,您可以使用
输出文件.write()
替换
格式化的\u-seq+=
,而不是直接将字符串收集到输出文件的变量中。但是我的想法是对代码进行一些清理,使其更具可读性。嗨,我在让您的代码正常工作方面仍然有一些问题,我是不是做错了什么?我有一张照片。我不太明白如何使用格式化的_seq+=而不是output_file.write(),是否必须将序列另存为其他内容?@JacobSørensen 1。在读取之前,请先关闭
input\u文件
。如果关闭输入文件,变量
序列
将无法读取该文件。因此,在
for
循环之后关闭它。2.您必须通过
output\u file将结果字符串
formatted\u seq
写入输出文件。write(formatted\u seq)