我已经搜索了最近几天试图找到此帮助,并没有发现任何东西。如何在Python中读取CSV文件中的特定行
我正在学习A Level计算机科学。该项目是一个货币转换器,我想知道如何让用户输入货币代码,然后让程序获取这些信息并将其与CSV文件进行比较。如果货币代码在文件中,那么我需要程序获得与来自输入的货币代码相匹配的转换率,并在总和/等式中使用此值来完成转换。我尝试了几种不同的方法来尝试和实现一个CSV文件,但我似乎无法让它工作。
我使用Python 3.5.2
我不要求对整个代码只是如何实现这样一个CSV文件的一些例子。
这是我的CSV文件中的一个例子:
Currency, Code, Rate
Canadian Dollar, CAD, 1.3457
Swiss Franc, CHF, 1.0129
British Pounds, GBP, 0.8056
Japanese Yen, JPY, 111.52
Bitcoin, BTC, 0.001351
我的第一个节目,如果使用的elif和else语句但是来实现转换,因为我这么早就有人告诉我,使用完成任务而不是CSV文件。
这是初始代码:
def Amount_Input():
while True:
try:
global amount
amount = float(input("Enter amount to be converted:"))
Currency_From()
break
except ValueError:
print("Invalid Entry, Please enter a valid entry as a decimal number.")
continue
def Currency_From():
currencyInput1 = input("Enter the currency you wish to convert from:")
if currencyInput1 in ['USD', 'usd']:
USD()
elif currencyInput1 in ['GBP', 'gbp']:
GBP()
elif currencyInput1 in ['EUR', 'eur']:
EUR()
elif currencyInput1 in ['BTC', 'btc']:
BTC()
else:
print("Invalid entry")
Currency_From()
def USD():
currencyInput2 = input("Enter the currency you want to convert to:")
if currencyInput2 in ['GBP', 'gbp']:
print("You are converting", amount, "USD to GBP.")
converted_amount = amount*0.81
print(converted_amount)
elif currencyInput2 in ['EUR', 'eur']:
print("You are converting", amount, "USD to EUR.")
converted_amount = amount*0.94
print(converted_amount)
elif currencyInput2 in ['BTC', 'btc']:
print("You are converting", amount, "USD to BTC.")
converted_amount = amount*0.0013
print(converted_amount)
else:
print("Invalid Entry")
USD()
def GBP():
currencyInput2 = input("Enter the currency you want to convert to:")
if currencyInput2 in ['USD', 'usd']:
print("You are converting", amount, "GBP to USD.")
converted_amount = amount*1.24
print(converted_amount)
elif currencyInput2 in ['EUR', 'eur']:
print("You are converting", amount, "GBP to EUR.")
converted_amount = amount*1.17
print(converted_amount)
elif currencyInput2 in ['BTC', 'btc']:
print("You are converting", amount, "GBP to BTC.")
converted_amount = amount*0.0017
print(converted_amount)
else:
print("Invalid Entry")
GBP()
def EUR():
currencyInput2 = input("Enter the currency you want to convert to:")
if currencyInput2 in ['USD', 'usd']:
print("You are converting", amount, "EUR to USD.")
converted_amount = amount*1.06
print(converted_amount)
elif currencyInput2 in ['GBP', 'gbp']:
print("You are converting", amount, "EUR to GBP.")
converted_amount = amount*0.85
print(converted_amount)
elif currencyInput2 in ['BTC', 'btc']:
print("You are converting", amount, "EUR to USD.")
converted_amount = amount*0.0014
print(converted_amount)
else:
print("Invalid Entry")
EUR()
def BTC():
currencyInput2 = input("Enter the currency you want to convert to:")
if currencyInput2 in ['USD', 'usd']:
print("You are converting", amount, "BTC to USD.")
converted_amount = amount*746.20
print(converted_amount)
elif currencyInput2 in ['GBP', 'gbp']:
print("You are converting", amount, "BTC to GBP.")
converted_amount = amount*600.89
print(converted_amount)
elif currencyInput2 in ['EUR', 'eur']:
print("You are converting", amount, "BTC to EUR.")
converted_amount = amount*704.36
print(converted_amount)
else:
print("Invalid Entry")
BTC()
print(Amount_Input())
如果我这样做,我会在开始时读取整个文件,并创建一个映射货币代码的字典来进行评分。 –
也许你可以下载并检查它们的实现:https://pypi.python.org/pypi/CurrencyConverter/0.5 – MYGz