3

I'm slightly new to Python and have a question as to why the following code doesn't produce any output in the csv file. The code is as follows:

import csv
import urllib2

url = 'http://www.rba.gov.au/statistics/tables/csv/f17-yields.csv'
response = urllib2.urlopen(url)
cr = csv.reader(response)

for row in cr:
    with open("AusCentralbank.csv", "wb") as f:
        writer = csv.writer(f)
        writer.writerows(row)

Cheers.

Edit:

Brien and Albert solved the initial issue I had. However, I now have one further question. When I download the CSV File which I have listed above which is in "http://www.rba.gov.au/statistics/tables/#interest-rates" under Zero-coupon "Interest Rates - Analytical Series - 2009 to Current - F17" and is the F-17 Yields CSV I see that it has 5 workbooks and I actually just want to gather the data in the 5th Workbook. Is there a way I could do this? Cheers.

Jojo
  • 1,117
  • 2
  • 15
  • 28

3 Answers3

7

I could only test my code using Python 3. However, the only diffence should be urllib2, hence I am using urllib.respose for opening the desired url.

The variable html is type bytes and can generally be written to a file in binary mode. Additionally, your source is a csv-file already, so there should be no need to convert it somehow:

#!/usr/bin/env python3
# coding: utf-8

import urllib

url = 'http://www.rba.gov.au/statistics/tables/csv/f17-yields.csv'

response = urllib.request.urlopen(url)
html = response.read()

with open('output.csv', 'wb') as f:
        f.write(html)
albert
  • 8,027
  • 10
  • 48
  • 84
  • Thank you very much for your answer. I have made an addition to my initial question and was wondering if I could get your help. Thanks – Jojo Jun 10 '15 at 17:39
  • Please open a new question since this is not a forum but a question and answer platform. Keep in mind to provide a link to data you have problems with. However, I could not find the mentioned five workbooks in the csv-file(http://www.rba.gov.au/statistics/tables/csv/f17-yields.csv). The Excel-File has four workbooks containing the data of those three csv-files and additional notes. – albert Jun 10 '15 at 22:15
3

It is probably because of your opening mode.

According to documentation:

'w' for only writing (an existing file with the same name will be erased)

You should use append(a) mode to append it to the end of the file.

'a' opens the file for appending; any data written to the file is automatically added to the end.

Also, since the file you are trying to download is csv file, you don't need to convert it.

Lafexlos
  • 7,618
  • 5
  • 38
  • 53
  • I changed it to `with open("AusCentralbank.csv", "wb") as f`, yet I still don't get anyoutput in the CSV File. Do you know why this could be? – Jojo Jun 10 '15 at 16:45
  • You have a few issues with the way you've set up your program. See my or @albert's answer for a more complete way of doing things. – Blair Jun 10 '15 at 16:52
  • You re-open (i.e., destroy) your output file for every row of input. You end up with a probably empty row... – alexis Jun 10 '15 at 16:52
1

@albert had a great answer. I've gone ahead and converted it to the equivalent Python 2.x code. You were doing a bit too much work in your original program; since the file was already a csv you didn't need to do any special work to turn it into a csv.

import urllib2

url = 'http://www.rba.gov.au/statistics/tables/csv/f17-yields.csv'

response = urllib2.urlopen(url)
html = response.read()

with open('AusCentralbank.csv', 'wb') as f:
    f.write(html)
Blair
  • 6,623
  • 1
  • 36
  • 42
  • Sorry. I actually have one more request with regards to my initial Question. I have edited the original question to include this. – Jojo Jun 10 '15 at 17:33
  • At this point I would create a new question for it. I don't really think anyone besides me will see this since the question is old and marked as complete. I'm not sure of a way to do what you've requested. – Blair Jun 10 '15 at 17:50