Convert string to integer python CGI
Question: I'm stuck on a part of my code where I need to convert the value of a radio
button from a string into a int because the function the value goes into takes
an integer. When the radio button is selected and the user presses submit, I
get a string of that value when I need an integer. I've tried the basic
convert tactics in python like int() but I get a TypeError: int() argument
must be a string, a bytes-like object or a number, not 'list'. Are there any
other ways to convert this value to an int? For example when I print it out I
get ['14'] when I need 14.

My location_table script

    
    
    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 ('''
        <!DOCTYPE html>
        <html>
            <head>
                <title>{}</title>
                <link rel="stylesheet" type="text/css" href="/style1.css"/>
            </head>
            <body>
        '''.format("Areas"))
    
        def name_key(x):
                return x['location_id']
    
    
    
        print("<form method='get' action='measurement_table.py'>")
        print("<name='area'>")
    
        print("<table class='grid'>")
        row_template = "<tr>" \
                       "<td>{}</td>" \
                       "<td>{}</td>" \
                       "<td>{}</td>" \
                       "<td>{}</td>" \
                       "</tr>"
        print("<tr><th>Select</th><th>ID</th><th>Name</th><th>Altitude</th></tr>")
        for x in sorted(location, key=name_key):
            location_id = x['location_id']
            name = x['name']
            alt = x['altitude']
            radio = "<input type='radio' name='location_id' value={:.0f}>".format(location_id)
            print(row_template.format(radio,location_id, name, alt ))
        print("</input>")
        print("</table>")
        print("<p>"
              "<input type='submit' value='Get Measurement Information'>"
              "</p>")
    
        print('''
          </body>
        </html>
    ''' )
    
    if __name__=="__main__":
        page()
    

my measurements_table script that location_table links to when the user
presses submit

    
    
    import cgi
    import cgitb
    cgitb.enable()
    from A3 import db_access
    from area_selection import page
    fs = cgi.FieldStorage()
    
    location_id = fs.getlist('location_id')
    byte = int(location_id[0])
    
    if len(location_id)==0:
        page("Exactly 1 selected")
        quit()
    try:
        area_id= int(location_id[0])
    except Exception as exc:
        page("area id should be valid int: " + str(location_id))
        quit()
    
    mea = db_access.get_measurements_for_location(location_id)
    

The problem i'm having from location_table code:

    
    
        radio = "<input type='radio' name='location_id' value={}>".format 
    

And in measurement_table where i'm having the conversion error:

    
    
    mea = db_access.get_measurements_for_location(location_id)
    

The Error: [In the URL address, you can see the location_id number i'm trying
to convert](http://i.stack.imgur.com/JNP3t.png)


Answer: You have a list. Presumably the first item of that list is the value of the
selected radio button. You can get the first item of a list using list
indexing, like this:

    
    
    location_id = ['14']
    location_id = int(location_id[0])
    

If this hasn't helped you then add your CGI code to your question as this is
where the problem really is.

