我对python的世界相当新鲜,感觉好像我对我制作的脚本过于复杂,是否有任何可以简化的方法或任何可以合并的技术#&# 39; m不知道? (如果这过于模糊,请道歉)
#!/usr/bin/python
import sys, urllib2, re
es="http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=protein&term="+sys.argv[1]
ef="http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=protein&id="
url=urllib2.urlopen(es)
match=url.readlines()
print "<head>"
print "<style>"
print "table, th, td {"
print " border: 1px solid black;"
print "}"
print "</style>"
print "</head>"
print "<body>"
print "<table>"
print "<tr>"
print "<th>GI number</th>"
print "<th>ID</th>"
print "<th>Nucleotide sequence</th>"
print "</tr>"
for line in match:
col1=re.search(r'<Id>(.*)</Id>',line)
if col1:
fetch=ef+col1.group(1)+"&rettype=fasta&retmode=xml"
urlf=urllib2.urlopen(fetch)
print "<tr>"
print "<th>"
print "<a href=\"http://www.ncbi.nlm.nih.gov/protein/"+col1.group(1)+"\">"+col1.group(1)
print "</a>"
print "</th>"
pline="".join(urlf.readlines())
col2=re.search(r'<TSeq_sequence>(.*)</TSeq_sequence>',pline)
col3=re.search(r'<TSeq_defline>(.*)</TSeq_defline>',pline)
if col3:
print "<td>"
print col3.group(1)
print "</td>"
ltot=(len(col2.group(1))/40)+1
ln=0
print "<td>"
while ln<=ltot:
bnd1=ln*40
bnd2=(ln+1)*40
print col2.group(1)[bnd1:bnd2]
ln+=1
print "</tr>"
print "</body>"
print "</table>"
答案 0 :(得分:0)
您可以使用多行字符串创建大块代码,而不是使用每行html代码的打印行,例如:
topSection = """<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th>GI number</th>
<th>ID</th>
<th>Nucleotide sequence</th>
</tr>"""
对于需要以编程方式插入某些值的html代码部分,您可以使用格式字符串方法,例如:
variable1 = "some value"
variable2 = "some other value"
htmlsection = """<HTML CODE>
<MORE HTML CODE>
<MORE HTML CODE>
<HTML CODE WITH A VARIABLE VALUE = {0}>
<HTML CODE WITH ANOTHER VARIABLE VALUE = {0}>""".format(variable1, variable2)
然后,您可以连接所有字符串组件,只使用一个打印语句:
print(topSection + middleSection + endSection)