python字符转成整形_将字符串转换为整数python CGI

我的代码中有一部分需要将单选按钮的值从字符串转换为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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值