我已經搜索了最近幾天試圖找到此幫助,並沒有發現任何東西。如何在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