使用CGI模块时,如果循环未执行,则在循环内打印语句

2024-10-03 17:16:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个程序,它将使用cgi从HTML页面获取信息,然后将值传递给for循环,循环将在excel表中搜索条目并打印与搜索条件匹配的列。你知道吗

我的程序看起来像下面的那个,但是它不会在每次for循环之后打印搜索结果,当使用CGI接收到值时,所以“print(domain)”不会被执行。你知道吗

#!c:\Python27\python.exe

import openpyxl
import cgi, cgitb

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "</body>"

formData=cgi.FieldStorage()
Mode=formData.getvalue('uniqvalue')
Mode_it={Mode}

wb=openpyxl.load_workbook('excelsheet.xlsx')
sheet= wb.get_sheet_by_name('Sheet1')

for rowNum in range (2, sheet.max_row):
    invName=sheet.cell(row=rowNum,column=2).value
    if invName in Mode_it:
         domain=sheet.cell(row=rowNum, column=3).value
         print(domain)

print "<h4> You searched for %s</h4>" % Mode

print "</body>"
print "</html>" 

有趣的是,如果我做了下面的更改,更改变量模式并给出一个固定值,它就可以完美地工作。你知道吗

基本上,如果我把变量模式的值设为7961,它就工作了:

formData=cgi.FieldStorage()
Mode=formData.getvalue('uniqvalue')
Mode_it={7961}

我知道循环是有效的,最后一个print语句打印我在HTML中输入的值的输出,所以我知道它也有效。你知道吗

我不知道为什么当它是一个变量的时候它不会。任何线索都将不胜感激。你知道吗

我使用的示例HTML具有以下语法:

<form action="Searchandreturn.py" method="post">
Enter the Value: <input type="text" name="uniqvalue"><br />
<input type="submit" value="Submit" />
</form> 

Tags: forvaluemodedomainhtmltypeitsheet