简体   繁体   English

读取csv文件时遇到问题

[英]Having trouble with reading a csv file

I'm new to Python,and I don't have much experience with the libraries. 我是Python的新手,我对库没有太多的经验。 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. 我正在尝试使用我的货币转换器程序的“请求”模块从“ www.bankofcanada.com”获取CSV文件。我想读取该文件并进行解析,以获取货币及其比率。在字典中使用它们。 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:( 这两个部分分别可以正常工作(我可以获取CSV文件并保存它,并且可以在已有文件的情况下根据需要解析CSV文件)。我的问题是它们不能一起工作并给我空结果:

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形式让Python自动处理此问题:

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. with open格式也更安全,因为如果您手动进行操作,很容易忘记关闭文件处理程序。

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. Python具有许多不错的语言功能,旨在使开发人员的生活更轻松,而这些功能不一定与其他语言直接相同。 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. 人们谈论“ Idomatic Python”(这是一件好事),这意味着他们使用的是内置的快捷方式,而不是自己动手编写。

*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. 这有助于防止文件内容被其他进程所破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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