带有 csv 文件的 Python,货币转换

问题描述 投票:0回答:0

我想根据日期使用不同的货币汇率打印不同的数字。

import csv

def load_exchange_rates(test1): exchange_rates = {} with open(test1, newline='') as file: reader = csv.reader(file, delimiter=';') next(reader) for row in reader: if '' in row[1:]: continue date = row[0] exchange_rates['EUR'] = float(row[1]) exchange_rates['AUD'] = float(row[2]) exchange_rates['GBP'] = float(row[3]) return exchange_rates

def u_to_e(exchange_rates): usd = float(input("Give USD to convert: ")) print(round(usd, 1), "USD in EUR is", round(usd*exchange_rates['EUR'], 2), "EUR")

def u_to_a(exchange_rates): usd = float(input("Give USD to convert: ")) print(round(usd, 1), "USD in AUD is", round(usd*exchange_rates['AUD'], 2), "AUD")

def u_to_g(exchange_rates): usd = float(input("Give USD to convert: ")) print(round(usd, 1), "USD in GBP is", round(usd*exchange_rates['GBP'], 2), "GBP")

while True: print("ACME(tm) US DOLLAR EXCHANGE RATE APP") print("1) LOAD currency exchange rate data from a file") print("2) USE AVERAGE exchange rate") print("3) USE HIGHEST exchange rate") print("4) USE LOWEST exchange rate") print("5) CONVERT USD TO EUR") print("6) CONVERT USD TO AUD") print("7) CONVERT USD TO GBP") print("0) QUIT program")

choice = input("Choose what to do: ")

if choice == '1':
    f_name = input("Give name of the data file: ")
    with open(f_name, 'r') as f:
        reader = csv.reader(f, delimiter=';')
        data = list(reader)
        if len(data) > 1:
            start_date = data[1][0]
            end_date = data[-1][0]
            days = len(data) - 1
            print(f"Data loaded successfully!\nCurrency exchange data is from {days} days between {start_date} and {end_date}.\n")
            exchange_rates = load_exchange_rates(f_name)

elif choice == '2':
    print("Using the average currency exchange rate.")
    print("")
    
elif choice == '3':
    print("Using the highest currency exchange rate.")
    print("")
    
elif choice == '4':
    print("Using the lowest currency exchange rate")
    print("")

elif choice == '5':
    u_to_e(exchange_rates)
    print("")

elif choice == '6':
    u_to_a(exchange_rates)
    print("")

elif choice == '7':
    u_to_g(exchange_rates)
    print("")

else:
    break

ACME(tm) 美元汇率应用↩

  1. `从文件中加载货币汇率数据↩
  2. 使用平均汇率↩
  3. 使用最高汇率↩
  4. 使用最低汇率↩
  5. 将美元兑换成欧元↩
  6. 将美元兑换成澳元↩
  7. 将美元兑换成英镑↩
  8. 退出计划↩ 选择要做什么:5↩ 给美元兑换:1↩ 1.0 美元兑 0.81 欧元↩`

ACME(tm) 美元汇率应用↩

  1. LOAD currency exchange rate data from a file↩
  2. USE AVERAGE exchange rate↩
  3. USE HIGHEST exchange rate↩
  4. USE LOWEST exchange rate↩
  5. CONVERT USD TO EUR↩
  6. CONVERT USD TO AUD↩
  7. CONVERT USD TO GBP↩
  8. QUIT program↩ Choose what to do: 5↩ Give USD to convert: 1↩ 1.0 USD in EUR is 0.81 EUR↩

ACME(tm) 美元汇率应用程序↩ 1) LOAD currency exchange rate data from a file↩ 2) USE AVERAGE exchange rate↩ 3) USE HIGHEST exchange rate↩ 4) USE LOWEST exchange rate↩ 5) CONVERT USD TO EUR↩ 6) CONVERT USD TO AUD↩ 7) CONVERT USD TO GBP↩ 0) QUIT program↩ Choose what to do: 5↩ Give USD to convert: 1↩ 1.0 USD in EUR is 0.88 EUR↩

ACME(tm) 美元汇率应用↩

  1. LOAD currency exchange rate data from a file↩
  2. USE AVERAGE exchange rate↩
  3. USE HIGHEST exchange rate↩
  4. USE LOWEST exchange rate↩
  5. CONVERT USD TO EUR↩
  6. CONVERT USD TO AUD↩
  7. CONVERT UST TO GBP
  8. QUIT program↩ Choose what to do: 5↩ Give USD to convert: 1↩ 1.0 USD in EUR is 0.93 EUR↩

DATE;USD-EUR;USD-AUD;USD-GBP 1.1.2020;;; 2.1.2020;0.893416;1.430001;0.757867 3.1.2020;0.897102;1.438145;0.763569 4.1.2020;;; 5.1.2020;;; 6.1.2020;0.893336;1.439968;0.761256 7.1.2020;0.895095;1.454619;0.762469 8.1.2020;0.899685;1.45704;0.763545 9.1.2020;0.90009;1.457516;0.767642 10.1.2020;0.901632;1.454513;0.764674 11.1.2020;;; 12.1.2020;;;

我尝试根据日期使用不同的货币,但似乎总是使用相同的货币汇率。如何解决?

python csv currency
© www.soinside.com 2019 - 2024. All rights reserved.