感谢昨天来自stackoverflow的一些帮助,我的代码取得了进展。但我有一个关于我的页面的问题。这是我的代码
#!/usr/bin/python
print 'Content-type: text/html\n'
print
#May 17
import cgi, cgitb
cgitb.enable()
form=cgi.FieldStorage()
#May19
dataset1=open('Book1.csv','r')
dataset2=open('Race Demographic by state.txt','r')
sources='''<hr><h4>Sources!</h4><a href="http://en.wikipedia.org/wiki/Demographics_of_the_United_States">Race Demographics</a>
<a href="http://voices.yahoo.com/average-sat-scores-state-2010-7702520.html?cat=4">SAT Scores</a>
<a href="http://lisa.stuy.edu/~keiran.carpen/BriansDirectory/Sat scores by state.txt">SAT Scores txt file</a>
<a href="http://lisa.stuy.edu/~keiran.carpen/BriansDirectory/Race Demographic by state.txt">Race Demographics txt file</a></body></html>'''
def datasplitter(x):
data = x.read()
return data.split("\n")
def datdatamang(x):
data = datasplitter(x)
index = 0
WhileNumber = 0
while index < len(data):
WhileNumber = 0
string = data[index]
string.split(" ")
x=''
while WhileNumber < len(string):
if string[WhileNumber] == ',':
x=x+'</td><td>'
else:
x=x+string[WhileNumber]
WhileNumber+= 1
' '.join(string)
data[index]='<tr><td>'+x+'</td></tr>'
index+=1
result=' '.join(data)
result='''<table border='1'>'''+result+'</table>'
return result
#May 19
def getDescription():
page = ''
state = ''
#May20
if 'Drop' in form:
if form['Drop'].value =="Description":
page+= 'Ever since its conception, the SAT examinations have been widely controversial.Many individuals believe that their race is the most intellectually superior, and that their SAT scores reflect on their superiority. It is my job to find this correlation between scores and race and end the battle among the races!'
if form['Drop'].value=="High SAT Scores":
page+= datdatamang(dataset1)
if form['Drop'].value=="Race Demographics":
page+= datdatamang(dataset2)
if form['Drop'].value=="source":
page+= sources
else:
return page
#May 21
def getState():
table=''
if 'on' in form:
state+= form['on'].value
if state in dataset1:
state.header=dataset1.index(state)
for n in dataset1[state.header]:
table+='''<tr>'''+n+'''</tr>'''
return '''<td>'''+table+'''</td>'''
def getRacebyState():
if 'on' in form:
state+= form['on'].value
if state in dataset2:
state.header=dataset2.index(state)
for n in dataset2[state.header]:
table+='''<tr>'''+n+'''</tr>'''
return '''<td>'''+table+'''</td>'''
#May 20
print '''<html><title>SAT Analysis</title>'''
print '''<body>'''
print getDescription()
print '''</body></html>'''
#May 17 - Writing the analysisV2.html file and starting the code
#May 19 - Tables, and the like
#May 20 - Writing if statements for drop-down menu, building the page.
#May 21 - Working on text fields and getState
从本质上讲,我的页面可以正常工作,因此您可以从下拉菜单中进行选择(选择以下值之一:例如“高 SAT 分数”和“种族人口统计”会导致我的 python 代码生成一个包含表格或描述的页面)下拉菜单选项)或文本字段(在我的 CSV 文件中搜索状态,并返回包含有关该特定状态的数据的表格行)。使用 cgi.FieldStorage() Python 收集通过 HTML 表单提交的值。但是,如何编写代码,以便仅通过 HTML 表单从文本字段发送值。
即,如果我不想使用下拉菜单,而只想使用文本字段来查找特定的状态,而不是通过下拉菜单提交表单数据,我该怎么做?