2015-11-04 61 views
0

我給出以下骨架讀取文件和相關信息

#This function helps calculate each transactions and return the final balance 
#PARAMETERS: 
#filename: the file going to be read, e.g. "transactions.txt", type: string 
#namelist: a list containing all company names, type: list 
#orgBalance: stores original balance for each company, type: nested list 
#url: the url you are trying to fetch, type: string 
#RETURN: current balance information (after all transactions), type: nested list 
def transaction(filename, namelist, orgBalance, url): 

    #Call fetch(url) to get currency exchange information 
    exchange_info= fetch(url) 
    #Read each line from transactions.txt 
    myFile = open(filename,'r') 
    data=myFile.readlines() 
    #Check which company is conducting transactions 

    #If BUY, then convert the amount of foreign currency to USD 
    #and subtract the calculated amount 

    #If SELL, then convert the amount of foreign currency to USD 
    #and add the calculated amount 

    #Return current balance list 
    #your list should look like 
    #[['Acer', 481242.74], ['Beko', 966071.86], ...] 

,我的數據列表如下:

transaction.txt

Gerdau BUY Brazilian Real: 17454 
Gerdau SELL Botswana Pula: 31162 
Acer BUY Danish Krone: 61376 
Equifax BUY Icelandic Krona: 41983 
Acer BUY Sri Lankan Rupee: 91659 
Datsun SELL Trinidadian Dollar: 71248 
Haribo BUY Indonesian Rupiah: 41548 
Datsun SELL Saudi Arabian Riyal: 71627 

namelist=['Acer', 'Beko', 'Cemex', 'Datsun', 'Equifax', 'Gerdau', 'Haribo'] 
orgBalance=[['Acer', 481242.74], ['Beko', 966071.86], ['Cemex', 187242.16], ['Datsun', 748502.91], ['Equifax', 146517.59], ['Gerdau', 898579.89], ['Haribo', 265333.85]] 
and the url=https://www.cs.purdue.edu/homes/jind/exchangerate.html 

我只是想知道,如果有人能通過這個步驟來引導我,我會得到前幾個部分,但是一旦我得到第三個評論我就會失去。我不知道如何處理搞清楚哪些公司購買或出售

+3

歡迎來到StackOverflow。請閱讀並遵守幫助文檔中的發佈準則。 [主題](http://stackoverflow.com/help/on-topic)適用於此處。 SO不是教程服務,儘管您可能會在其他網站上找到幫助。 – Prune

+1

這裏的問題目前還不清楚。 – eljefedelrodeodeljefe

+0

我知道這個網站不是關於教程,所以我想我應該定義一個問題,在代碼中的第三個評論它說檢查哪個公司是傳導業務,那麼我將如何通過交易文本並找出並排序哪些公司買賣? –

回答

0
bank = dict(orgBalance) 
for line in data: 
    company,action,currency,ammount = line.split() 
    do_something(company,action,currency,ammount,bank) 

,那麼你將需要編寫一個函數do_something實際貨幣轉換成美元,然後要麼添加或從公司銀行賬戶中減去

def find_currency_rate(currency): 
    # locate the curency name in the text body and find the last <td></td> value in that row... 
    return float(last_td_cell_of_row) 

def convert_to_usd(currency,amount): 
    currency_rate = find_currency_rate(currency) 
    return amount*currency_rate 

def do_something(company_name,action,currency_code,amount,bank): 
    amount_in_usd = convert_to_usd(currency_code,amount) 
    if action == "BUY": 
     bank[company_name] = bank[company_name] - amount_in_usd 
    else: # else we sell and add the funds to our bank 
     bank[company_name] = bank[company_name] + amount_in_usd 
+0

我是主要難以通過信息獲取和整理。所以就像我不知道如何得到轉換率,如如何從列表中獲取數據 –

+0

我懷疑這是你應該學習的課程的一部分...但是,如果你想要一個解決方案,我當然可以添加一個 –

+0

繼承人我的問題,即時營銷主要卡在上層編程類,我認爲Python是偉大的,我懂得如何編寫簡單的代碼,並寫了很多我的代碼到這一點,我只是有很多困難與排序數據,我知道如何找到,打開,並閱讀文件,我知道如何把它放入一個字符串,我可以拆分字符串基於買入和賣出,但我不知道如何分割它,使輸出將是公司名稱購買,以便稍後可用。我不知道如何讓它流通,所以整個功能工作 –