2017-06-23 57 views
-2

我有文字 「podaci.txt」 文件是這樣的:如何顯示某一行的部分?

Name|Lastname|1-000-1|1234|5000 
Name1|Lastname1|1-000-1|4321|15500 

12344321的PIN碼。

而且這樣的代碼:

def ProveraStanja(self): 
    file_data = open("podaci.txt").readlines() 
    for i in file_data: 
     i=i.strip("\n").split("|") 
     account_balance=i[4] 
    self.top = Toplevel() 
    self.la = Label(self.top,text="Acaunt balance: ") 
    self.la.grid() 
    self.Results = Label(self.top, text = account_balance) 
    self.Results.grid() 
    self.bt = Button(self.top,text='Potvrdi', command = self.potvrdiBtn) 
    self.bt.grid() 
    self.top.resizable(0,0) 

如何顯示賬戶餘額爲登錄的用戶?

我的問題是,python總是隻顯示最後一個用戶的accaunt平衡。

例如,如果我登錄爲1234,它仍然會顯示15500我的帳戶

+0

你不檢查,看看當您的線路有你的拉鍊和突破。 –

回答

0

與您的代碼的問題是,不要將你的每一行的數據。相反,你用For循環的每一次迭代覆蓋account_balance。因此,只保留最後一個值,這與您文件中的最後一行相對應。

你能做到像這樣:

def getBalance(myPin): 
    with open("podaci.txt", "r") as file: 
     for line in file.readlines(): 
      split_line = line.strip().split("|") 
      if split_line[3] == myPin: 
       return split_line[4] 
    return 0 #Return 0 balance if the PIN does not exist 

我們在這裏做的是以下幾點:

  1. 打開使用with聲明,你可以看到 here文件。
  2. 閱讀文件中的所有行並循環瀏覽它們。
  3. 使用'|'字符作爲我們的分隔符分割每一行。
  4. 檢查列表中的第4項(索引3)是否與myPin相同。

    • 如果是這樣,返回第5項,這是餘額。如果沒有,則返回0。

當然,這個解決方案很不理想。我儘可能簡單地使它變得簡單易懂。有很多方法可以改進它。例如,你應該添加某種形式的冗餘。

def getBalance(myPin): 
    with open("podaci.txt", "r") as file: 
     for line in file.readlines(): 
      split_line = line.strip().split("|") 
      if len(split_line) == 5: #Make sure that the line is valid 
       if split_line[3] == myPin: 
        return split_line[4] 
    return 0 #Return 0 balance if the PIN does not exist 

我在這裏做的是我加入了一個額外的if聲明,確保該行具有預期的有效入境物品的數量。這樣,讀取空行或無效的行不會導致異常。

然而,絕對是最好的解決方案(除了使用適當的數據庫)是使用class來保存我們的條目數據:

class Account: 
    def __init__(self, list): 
     self.Name = list[0] 
     self.LastName = list[1] 
     self.AccountNumber = list[2] 
     self.Pin = list[3] 
     self.Balance = list[4] 

accounts = [] #Accounts are all kept here 

#Load all accounts from the file 
def getAccounts(): 
    with open("podaci.txt", "r") as file: 
     for line in file.readlines(): 
      split_line = line.strip().split("|") 
      if len(split_line) == 5: #Make sure that the line is valid 
       accounts.append(Account(split_line)) 

#Find the account that matches the inputted PIN 
def findBalance(myPin): 
    for account in accounts: 
     if account.Pin == myPin: 
      return account.Balance 

這樣一來,我們的數據是更加有組織,並添加新的特性是更簡單。對於最後一個例子,我還修改了代碼來讀取文件並只加載一次帳戶,這在沒有使用類的情況下很難做到。

只是爲了好玩,你可以把它更進一步,將所有的字符串處理實際的類:

class Account: 
    def __init__(self, name, lastName, accountNumber, pin, balance): 
     self.Name = name 
     self.LastName = lastName 
     self.AccountNumber = accountNumber 
     self.Pin = pin 
     self.Balance = balance 

    def Decode(line): 
     split_line = line.strip().split("|") 
     if len(split_line) == 5: #Make sure that the line is valid 
      return Account(split_line[0], split_line[1], split_line[2], split_line[3], split_line[4]) 

accounts = [] 

def getAccounts(): 
    with open("podaci.txt", "r") as file: 
     for line in file.readlines(): 
      accounts.append(Account.Decode(line)) 
0

您遍歷文件中的所有行,但始終覆蓋account_balance。

創建account_balance作爲字典,如:

account_balance = {} 

,並在for循環店在正確的領域的平衡。

account_balance[i[3]] = i[4] 

訪問值,用鑰匙

account_balance[1234] 
相關問題