2016-11-27 259 views
-1

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

如果我這樣做,我會在開始時讀取整個文件,並創建一個映射貨幣代碼的字典來進行評分。 –

+0

也許你可以下載並檢查它們的實現:https://pypi.python.org/pypi/CurrencyConverter/0.5 – MYGz

回答

0

這裏的documentation on using csv.reader

可以在他們給它逐行讀取第一個例子中看到。 (你不應該需要指定分隔符或quotechar如果它是一個正常的CSV) 爲您的文件應該是這個樣子:

import csv 
with open('currency.csv', 'rb') as csvfile: 
    currencyreader = csv.reader(csvfile) 
    for row in currencyreader: 
     print row 

和輸出將是:

['Currency', 'Code', 'Rate'] 
['Canadian Dollar CAD 1.3457', 'CAD', '1.3457'] 
['Swiss Franc CHF 1.0129', 'CHF', '1.0129'] 
['British Pounds GBP 0.8056', 'GBP', '0.8056'] 
['Japanese Yen JPY 111.52', 'JPY', '111.52'] 
['Bitcoin BTC 0.001351', 'BTC', '0.001351'] 

你設定您打開的文件到csvfile對象。然後將文件的內容讀入到currencyreader對象中。然後逐行循環。

您必須將讀取用戶輸入的功能更改爲將行作爲參數。

此外,這可能只是個人偏好,但我會重構您的代碼,以刪除while True,因爲它們讓我畏縮。

0

我知道一個學校項目,你需要練習你的Python技能,但這樣的東西已經被現有的庫捕獲。對於任何有數字的東西,Numpy可能都是圖書館的第一名。這是我會怎麼對付你的項目:

from io import StringIO 
import numpy as np 

以下行做所有文件讀取和解碼

d = np.recfromcsv('currencies.csv', delimiter=',') 

現在你可以使用「d」來訪問您的個人幣種。

print(d[1]) # shows: (b'Swiss Franc', b'  CHF', 1.0129) 
print(d[1][2])# shows: 1.0129