Rio Rio - 3 months ago 26
Python Question

Formatting a variable as a string

I am trying to create a Python program that will query a URL and produce a JSON file. I need to pass an argument in the end of the URL which comes from the SQL query.

I have imported the

requests
library.

I get an error message 'TypeError: float argument required, not str' when I am trying to pass argument in the URL.

There is only one column of result produced from the query:

id
2
3
4


Below is what I came up with:

import MySQLdb
import requests

con=MySQLdb.connect(host="localhost",user="test", passwd="test", db ="mfg")
cur = con.cursor()
select=("select id from tblMfg")

cur.execute(select)
result=cur.fetchall()
for i in result:
col =i[0]
col1=str(col)
url = 'http://appl.xyz.net:8080/app/content/pq/doQuery?solution=nd&path=&file=Test.nd&dataAccessId=1&paramid=%d' % col1
user = 'abc'
password ='testgo'
data = requests.get(url, auth=(user, password))
json_data=data.json()
print json_data

Answer

Leave creating parameters to the requests framework instead:

params = {'solution': 'nd', 'path': '', 'file': 'Test.nd', 'dataAccessId': '1',
          'paramid': str(col[0])}
url = 'http://appl.xyz.net:8080/app/content/pq/doQuery'
user = 'abc'
password ='testgo'
data = requests.get(url, auth=(user, password), params=params)