API Data in Python via KNIME

python
#1

Hey All!

I am very new to Python so I apologize up front!

I am building a tool in KNIME that pulls data from the BLS API and lets the user custom build a report via the QuickForm nodes. I can get it to work just fine with the quick forms and it will return the report which is fantastic.

My issue is I want to use startyear and end year as variables inside of the Python code so the use can pick them via the Quick Form nodes. But for some reason the API is not accepting them for some reason.

For reference this is the original code:

data = json.dumps({“seriesid”: [‘CUUR0000SA0’,‘SUUR0000SA0’],“startyear”:“2011”, “endyear”:“2014”})

As you can see inside my Python I am using a variable for “seriesid” and this works. It’s just the year variables that are note.

data = json.dumps({“seriesid”: [flow_variables[‘Series Id’]],“startyear”: [flow_variables[‘start_year’]], “endyear”: [flow_variables[‘end_year’]]})

Any thoughts and suggestions are greatly appreciated!!

Here is the workflow (use the Report Builder component):

0 Likes

#2

Done!

I kept hammering away and came up with this and it works great!

start_year=[flow_variables[‘start_year’]]
for i in range(0, len([flow_variables[‘start_year’]])):
start_year = int(start_year[i])
end_year=[flow_variables[‘end_year’]]
for i in range(0, len([flow_variables[‘end_year’]])):
end_year = int(end_year[i])
start = (start_year)
end = (end_year)
headers = {‘Content-type’: ‘application/json’}
data = json.dumps({“seriesid”: [flow_variables[‘Series Id’]], “startyear”: “%d” % start, “endyear”: “%d” % end})

3 Likes

closed #3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

0 Likes