【发布时间】:2014-03-24 13:55:54
【问题描述】:
我在名为 1.htm - 100.htm 的文件夹中有 100 个文件。 我运行此代码以从文件中提取一些信息,并将提取的信息放在另一个文件 final.txt 中。目前,我必须为 100 个文件手动运行该程序。我需要构建一个可以运行程序 100 次的循环,每个文件读取一次。 (请详细解释我需要在我的代码中进行的确切编辑)
下面是文件6.htm的代码:
import glob
import BeautifulSoup
from BeautifulSoup import BeautifulSoup
fo = open("6.htm", "r")
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
htmltext = fo.read()
soup = BeautifulSoup(htmltext)
#print len(urls)
table = soup.findAll('table')
rows = table[0].findAll('tr');
for tr in rows:
cols = tr.findAll('td')
for td in cols:
text = str(td.find(text=True)) + ';;;'
if(text!=" ;;;"):
bo.write(text);
bo.write('\n');
fo.close()
bo.close()
b= open("output.txt", "r")
for j in range (1,5):
str=b.readline();
for j in range(1, 15):
str=b.readline();
c=str.split(";;;")
#print c[1]
if(c[0]=="APD ID:"):
f.write(c[1])
f.write("#")
if(c[0]=="Name/Class:"):
f.write(c[1])
f.write("#")
if(c[0]=="Source:"):
f.write(c[1])
f.write("#")
if(c[0]=="Sequence:"):
f.write(c[1])
f.write("#")
if(c[0]=="Length:"):
f.write(c[1])
f.write("#")
if(c[0]=="Net charge:"):
f.write(c[1])
f.write("#")
if(c[0]=="Hydrophobic residue%:"):
f.write(c[1])
f.write("#")
if(c[0]=="Boman Index:"):
f.write(c[1])
f.write("#")
f.write('\n');
b.close();
f.close();
f.close();
print "End"
【问题讨论】:
-
另外,
for j in range (1,5):从未使用过?或者至少你不在任何地方使用j,并且标签缩进在多个地方完全错误..