Reputation: 31
I'm a beginner to Python and I have been trying to export my data to a csv file but I can't figure out how to get rid of all the brackets and my comma separated. Ideally I need two columns: one with all values for "count" and one with values for "month".
Any advice appreciated.
My code:
from sunlight import capitolwords
import requests
import csv
r = requests.get ('http://capitolwords.org/api/1/dates.json?phrase=guns&start_date=2011-
12-01&end_date=2013-01-
15&granularity=month&sort=count&apikey=ab02633fb17841d09f4c3660e0384ae5')
data = r.text
ifile = open('guns.csv', 'rb')
reader = csv.reader(data.splitlines(), delimiter=',')
for row in reader:
print row
Result:
['{']
[' "results": [']
[' {']
[' "count": 62.0', '']
[' "month": "201212"']
[' }', '']
[' {']
[' "count": 36.0', '']
[' "month": "201207"']
[' }', '']
[' {']
[' "count": 35.0', '']
[' "month": "201112"']
[' }', '']
[' {']
[' "count": 27.0', '']
[' "month": "201202"']
[' }', '']
[' {']
[' "count": 27.0', '']
Upvotes: 3
Views: 10204
Reputation: 1307
I'd like to add a bit of explanation to what @thikonom has said above.
r.json()
When you're using r.json(), you're interpreting what is in r as json. A hint was given to your data being json in the query string you sent. If you're familiar with json, the results also look like json.
r.json()["results"]
You're now telling json to search your results r for the dictionary with the key: results. Being able to see what you're expecting from the query is a big help here, where results is shown in the results you've pasted into the window above. We also recognize that it's displayed in the standard way that a dictionary is shown:
{'results': [{'month': '201212', 'count': 62.0}...
and so on. results in this case is the key, the rest of the data after the : is considered the key's value. Using ~> r.json()["results"] <~ returns results value. However, you'll notice that 'month' and 'count' are also wrapped in {}, meaning they are each in themselves key, value pairs. This means you've got dictionaries nested within dictionaries!
f.writerow([elem["count"], elem["month"]])
Going through, row by row, pulling out the value for both keys: count and month, which you then store into your *.csv file.
Hope this may help somebody else!
Upvotes: 3
Reputation: 4267
Since the response is a Json, load the json data with:
data = r.json()["results"] # read the json response and keep the results part
and then write to the csv file:
with open("guns.csv", "wb") as csvfile:
f = csv.writer(csvfile)
f.writerow(["Count", "Month"]) # write the headers if you like
for elem in data:
f.writerow([elem["count"], elem["month"]])
Upvotes: 6
Reputation: 1343
if CSV package is not mandatory you can use the normal output operation and save the file with CSV. CSV is just a txt file with cols seperated by comma.
here how you can do this.
with open("abc.csv","w+") as fh:
str = "count: 62.0"
str = str + ",month: 201212"
fh.writeline(str)
Upvotes: 2