0

I'm new to python and really struggling with writing a program to download a CSV from this page So far I have:

import csv
import requests
import os

output_dir = 'C:/Users/Moshe/Downloads'
output_file = 'Covid_19_uk_timeseries.csv'

CSV_URL = 'https://coronavirus.data.gov.uk/downloads/csv/coronavirus-deaths_latest.csv'

assert os.path.exists(output_dir)# test that we can write to output_dir

with requests.Session() as s:
    download = s.get(CSV_URL)

    decoded_content = download.content.decode('utf-8')

    cr = csv.reader(decoded_content.splitlines(), delimiter=',')

    my_list = list(cr)
    for row in my_list:
        print(row)

Its output show that it gets the CSV fine, and has access to the output directory. But for now I just want to save it as it is, and can't seem to work out how to do that.

Abijah
  • 512
  • 4
  • 17

2 Answers2

1

You can use

csv.writerows()

to write the csv file. Check this site for more. https://www.programiz.com/python-programming/writing-csv-files

ashwin vinod
  • 156
  • 1
  • 1
  • 13
1
def main():

    import requests

    url = "https://coronavirus.data.gov.uk/downloads/csv/coronavirus-deaths_latest.csv"

    with requests.get(url, stream=True) as response:
        response.raise_for_status()

        with open("deaths_latest.csv", "wb") as file:
            for chunk in response.iter_content(chunk_size=8192):
                file.write(chunk)
            file.flush()

    print("Done")

    return 0


if __name__ == "__main__":
    import sys
    sys.exit(main())
Paul M.
  • 10,481
  • 2
  • 9
  • 15
  • Thanks! That works great. Could you add some comments so its easier for me to learn from what you've done? – Abijah May 20 '20 at 13:00
  • key questions: I don't get why your return statement becomes before the if statement, or how the if works. Also i played around with using this code to download other file types- and was suprised to see that it works. How does it work? – Abijah May 22 '20 at 16:42
  • @Abijah The `return` statement is the last line of the `main` function defined at the top, it's not part of the if-statement. If you're curious about how `if __name__ == "__main__"` works, read [this](https://stackoverflow.com/questions/419163/what-does-if-name-main-do) – Paul M. May 22 '20 at 16:47