2016-11-27 172 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