I am very new to Python and am trying to get a response back from the URL for every user in the user.csv file, return the JSON message, then parse it to a CSV. I'm not sure what to put in the return section or how to split out the message. This is a sample of the message that is returned to me when I get the HTTP request:
{"msg": {"mes": "four", "high": 1230, "low": 0}}"
Here is what I have so far but I am getting an error:
TypeError: expected string or buffer
import requests
import json
import csv
def getRows(data):
return []
url = 'http://test_url'
with open('user.csv', 'rU') as data_file:
data = csv.DictReader(data_file)
for row in data:
current_user = row['mdn']
r = requests.get(url)
data = json.loads(data)
fname = "mydata.csv"
with open(fname,'wb') as outf:
outcsv = csv.writer(outf)
outcsv.writerows(getRows(data))