I'm accessing an API and am then outputting the data to a CSV File as follows:
from urllib2 import Request, urlopen, URLError
import csv
request = Request('https://www.quandl.com/api/v1/datasets/YC/CHE10Y.csv? auth_token=AUTH_TOKEN') # Data available in CSV Format
try:
response = urlopen(request)
datafromfile = response.read()
with open('QuandlData.csv', 'w') as out:
writer = csv.writer(out)
for data in datafromfile:
writer.writerow(data);
except URLError, e:
print 'No data. Got an error code:', e
This is outputting data. However, the data is coming out like this:
D
a
t
e
,
R
a
t
e
"
"
2
0
1
5
-
0
6
-
0
5
,
0
I understand this is to do with the way I'm outputting the data into the CSV File. However, I don't know how to rectify this.
You are iterating over the characters returned from
response.read()
so only writing a char at a time. If you want to write lines:Or simply write
response.read()
with file.write.You are basically doing: