简体   繁体   中英

Having trouble with reading a csv file

I'm new to Python,and I don't have much experience with the libraries. I'm trying to get a CSV file from 'www.bankofcanada.com' using the "requests" module for my Currency converter program.I want to read the file,and parse it in order to get the currency and it's ratio, for using them in a dictionary. The 2 parts,individually work just fine (I can get the CSV file and save it,and I can parse the CSV file as I want when there's already a file).My problem is they dont work together and giving me empty results:(

import requests
import csv
import os
import time

rates = {


        }

os.chdir('C:\\Users\\Caroline\\Desktop')
res = requests.get("http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv")
csvFile = open('csvFile2.csv','wb')
for chunk in res.iter_content(10000):
    csvFile.write(chunk)
fh = open('csvFile2.csv')
fileReader = csv.reader(fh)
fileData = list(fileReader)
actual_data = fileData[7:]
for rows in actual_data:
    rates[rows[0]] = rows[-1]


print(rates)

Ps: I know there are lots of improvements to the code like not using absolute paths and ... This is just for debuggin purpose

You are not closing your file after writing it so there is nothing there to read. You need to do this for all file operations*. Use the the with open form to have Python handle this automatically:

with open('csvFile2.csv','wb') as csvfile:
    for chunk in res.iter_content(10000):
        csvFile.write(chunk)
with open('csvFile2.csv') as fh:
    fileReader = csv.reader(fh)

Here's the equivalent code closing the file handlers manually:

csvFile = open('csvFile2.csv','wb')
for chunk in res.iter_content(10000):
    csvFile.write(chunk)
csvFile.close()
fh = open('csvFile2.csv')
fileReader = csv.reader(fh)
fh.close()

As you can see it's longer with close. The with open format is also safer as its easy to forget to close the filehandler if you are doing it manually.

Python has lots of nice language features designed to make life easier as a developer, that don't necessarily have a direct equivalent in other languages. It is worth getting to know them. People talk about 'Idomatic Python' (as a good thing) and one of the things they mean by this is using these built-in shortcuts rather than rolling your own.

*Files need to be open and closed so they can be written (and read) safely. As a user the closing is always handled for us so its easy to think only in terms of opening a file, but as programmers we dealing with a lower level operation. When you write to a file nothing actually gets written to it until you close the file, even though it appears you are writing a line at a time in a loop. This helps prevent file contents getting clobbered by other processes among other things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM