2015-06-17 89 views
1

Inside fixtures.txt是英超聯賽下賽季裝置的內容。數據看起來像這樣:數據解析,pythonic方式

[email protected]:~/Desktop$ less fixtures.txt |head -n 4 
8 August 2015 
AFC Bournemouth v Aston Villa    #BOUAVL 
Arsenal v West Ham United    #ARSWHU 
Chelsea v Swansea City    #CHESWA 

我想排列每個團隊的裝置。我的方法看起來很糟糕,包括一堆線。更有效的方法是什麼?

teams = {'BOU' : 4, 'WAT' : 4, 'LEI' : 4, 'NOR' : 4, 'AVL' : 3, 'SUN' : 3, 'NEW' : 3, 'WBA' : 3, 'STK' : 2, 'SWA' : 2, 'EVE': 2, 'SOU' : 2, 'CPL' : 2, 'TOT': 2, 'ARS' : 1, 'CHE' : 1, 'MUN' : 1, 'LIV' : 1, 'MCI' : 1} 

fd = open("fixtures.txt", "r") 

for lines in fd: 
lines = lines.strip() 
matches = lines.split("#") 
if "CHE" in lines: 
    for k,v in teams.items(): 
     if k in matches[1]: 
      if "CHE" not in k: 
       print k,v 

輸出(切爾西的第一固定件):

​​

回答

2

什麼是最好的取決於你有多少數據要處理。在這種情況下,一半的問題是數據檢索和打印都混亂了。除非你正在處理大量的數據,否則建議將它們分開。

如果數據量(超過幾百場比賽更少)小,你可以閱讀所有的數據到一個列表,像這樣:

teams = {'BOU' : 4, 'WAT' : 4, 'LEI' : 4, 'NOR' : 4, 'AVL' : 3, 'SUN' : 3, 'NEW' : 3, 'WBA' : 3, 'STK' : 2, 'SWA' : 2, 'EVE': 2, 'SOU' : 2, 'CPL' : 2, 'TOT': 2, 'ARS' : 1, 'CHE' : 1, 'MUN' : 1, 'LIV' : 1, 'MCI' : 1} 

def read_fix(filename): 
    """Reads a named fixtures-file and returns a list containing pairs of team names [["BOU","AVL"],["ARS","WHU"],...]""" 
    matches=[] #create an empty list, this is part of a so called accumulator pattern 
    with open(filename, "r") as fd: #The with statement guarantees that the opened file is closed when the block ends. 
     for line in fd: 
      line = line.strip().split("#") #You can chain multiple method-calls on one line, this does both white-space-stripping and splitting on #. 
      if len(line)==2: #Filter out only lines that contain a game (or well exactly one #, possibly dangerous) 
       teams=line[1] #Remember that python indexes lists and strings from 0 
       matches.append([teams[0:3],teams[3:6]]) #split the part after the # into two parts, 3 letters each and add the pair to the list (this is the accumulation step of the accumulator-pattern) 
    return matches 

然後用另一個函數來打印:

def print_fix(games,team): 
    """Takes a list of games (as team-pairs) and prints information for the opposing team in each match that contains the specified team.""" 
    team=team.upper() #just for convenience 
    for game in games: 
     if team in game: #The in statement returns True if the team element is equal to at least one of the teams in the game-array. 
      #For example: team in ["CHE","LIE"] would return true if team was either "CHE" or "LIE" 
      if game[0] == team: #If "my team" is the first one of the pair, then the "Other team" must be the second, and the other way around. 
       other=game[1] 
      else: 
       other=game[0] 
      print other, teams[other] 

matches= read_fix("fixtures.txt") 
print_fix(matches,"CHE") 

一個更有效的方法是使用字典作爲臨時存儲,但我認爲這個代碼可能稍微容易閱讀。

+0

正是我在找什麼 – jester112358