处理从URL的CSV文件中提取的列表

时间:2018-10-12 22:41:43

标签: python csv user-interface dictionary file-handling

我正在尝试处理从在线CSV文件中提取的用于货币转换器GUI应用程序的数据。 CSV文件链接:({https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.zip)。

我已经使用split()提取了文件并将其从字符串转换为列表,接下来我要做的是为每种货币制作一个字典,其中日期为键,转换率为键的值。

我想要字典的示例:

USD: {
      (2018, 10, 8): 1.1478, 
      (2018, 10, 5): 1.1506, 
      (2018, 10, 4): 1.1502, 
      (2018, 10, 3): 1.1548, 
      (2018, 10, 2): 1.1543, 
      ...}

我需要将此字典与我的PyQt5小部件一起使用,以便执行转换并更新小部件。

应用程序中使用的库:

import sys
from PyQt5.QtWidgets import QLabel, QComboBox, QDoubleSpinBox, QCalendarWidget, QDialog, QApplication, QGridLayout
from PyQt5 import QtCore
from decimal import Decimal
from urllib.request import urlretrieve
import zipfile
import pyqtgraph as pg

我用来解压缩文件夹并将其转换为列表的代码:

self.data = {} 
url = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.zip'  
self.file, _ = urlretrieve(url)
zip_file_object = zipfile.ZipFile(self.file, 'r')
first_file = zip_file_object.namelist()[0]
self.file = zip_file_object.open(first_file)
content = self.file.read().decode()
lst = []
lst = content.split(",")

注意:这是使用Anaconda虚拟环境完成的

3 个答案:

答案 0 :(得分:1)

改善@codingatty的答案:

import csv
f = open("eurofxref-hist.csv")
csvreader = csv.reader(f)
header_row = next(csvreader)
currencies = header_row[1:]
all_currencies_dict = {}

for currency in currencies:
    all_currencies_dict[currency] = {}

for data_row in csvreader:
    date = data_row.pop(0)
    date_tuple = tuple(date.split("-"))
    for i, currency in enumerate(currencies):
        if data_row[i] != 'N/A':
            currency_dict = all_currencies_dict[currency]
            currency_dict[date_tuple] = data_row[i]

print(all_currencies_dict["USD"])

答案 1 :(得分:0)

据我所知,您所需要的就是您提到的那本词典。我已在此处为我的答案添加了评论,以便您可以按照我的操作

import pandas as pd
df = pd.read_csv("/Users/abhishekbabuji/Downloads/eurofxref-hist.csv")
df1 = df.copy() #Create copy of df
df1['Date'] = pd.to_datetime(df1['Date']) #Convert the 'Date' column in to datetime 

df1['Year'] = df1['Date'].dt.year #Creating seperate column containing year portion
df1['Month'] = df1['Date'].dt.month #Creating seperate column containing month portion
df1['Day'] = df1['Date'].dt.day #Creating seperate column containing day portion

df1['YMD'] = df1[['Year', 'Month', 'Day']].apply(tuple, axis=1) #Zipping these three into a tuple
USD = dict(zip(df1['YMD'],df1['USD'])) #Creating a dictionary with the tuple as key, and values are the rows in column named 'USD'

现在:

print(USD)

给予:

{(2018, 10, 12): 1.1574,
 (2018, 10, 11): 1.1575,
 (2018, 10, 10): 1.15,
 (2018, 10, 9): 1.1435,
 (2018, 10, 8): 1.1478,
 (2018, 10, 5): 1.1506,
 (2018, 10, 4): 1.1502,
 (2018, 10, 3): 1.1548,
 (2018, 10, 2): 1.1543,
 .
 .
 .}

答案 2 :(得分:0)

我将使用内置的csv模块,并使用标题行为您的货币词典设置键,然后填充数据行,而不是尝试遍历整个文件一串。

下面的内容直接读取CSV文件,但是您可以修改它来下载和解压缩该文件;你已经把那部分放下来了。为了清楚起见,有些冗长。 (Python3; next语法与Python2不同,但也应该可以。)

import csv

INFILE = "eurofxref-hist.csv"
f = open(INFILE)
csvreader = csv.reader(f)
header_row = next(csvreader)
currencies = header_row[1:] # remove the "Date" header entry; we want only the currencies
all_currencies_dict = {}
for currency in currencies:
    all_currencies_dict[currency] = {}
for data_row in csvreader:
    date = data_row.pop(0) # remove but save date
    date_tuple = tuple(date.split("-"))
    for i in range(0, len(currencies)):
        currency = currencies[i]
        currency_dict = all_currencies_dict[currency]
        conversion = data_row[i]
        currency_dict[date_tuple] = conversion
# remove empty entry resulting from spurious comma at end of each row:
del all_currencies_dict[""] 

现在您有一个dict格,每种货币一个。

print(all_currencies_dict["USD"])

将打印(为清晰起见而格式化):

{('2018', '10', '12'): '1.1574', 
('2018', '10', '11'): '1.1575',
 ...}