我的代码中有一部分需要将单选按钮的值从字符串转换为int,因为该值所包含的函数采用整数。当选中单选按钮并且用户按下submit时,当我需要一个整数时,我会得到一个该值的字符串。我尝试过python中的基本转换策略,比如int(),但是得到了一个TypeError:int()参数必须是字符串、类似对象的字节或数字,而不是“list”。有没有其他方法可以把这个值转换成int?
例如,当我把它打印出来的时候,我需要14时我得到了['14']。在
我的位置表脚本import cgi
import cgitb
cgitb.enable()
from A3 import db_access
from area_selection import page
fs = cgi.FieldStorage()
area_id = fs.getlist('area')
if len(area_id)==0:
page("Exactly 1 selected")
quit()
try:
area_id= int(area_id[0])
except Exception as exc:
page("area id should be valid int: " + str(area_id))
quit()
location = db_access.get_locations_for_area(area_id)
if not location:
page("area with id {} does not exist".format(area_id))
quit()
def page(*messages):
print("Content-Type: text/html; charset=UTF-8")
print("")
print ('''
{}'''.format("Areas"))
def name_key(x):
return x['location_id']
print("
")print("")
print("
row_template = "
" \"
{}" \"
{}" \"
{}" \"
{}" \"
"print("
SelectIDNameAltitude")for x in sorted(location, key=name_key):
location_id = x['location_id']
name = x['name']
alt = x['altitude']
radio = "".format(location_id)
print(row_template.format(radio,location_id, name, alt ))
print("")
print("
")print("
"
""
"
")print('''
''' )
if __name__=="__main__":
page()
当用户按下submit时,位置表链接到的MyMeasurements表脚本
^{pr2}$
我遇到的问题来自位置表代码:radio = "".format
在measurement_表中,我得到了转换误差:mea = db_access.get_measurements_for_location(location_id)