2014-09-19 74 views
2

在我开始之前,我想要注意的是,这是家庭作业,我不想在银盘上得到答案(不是我期望的) 。我需要我的逻辑帮助。阅读文本文件以查找特定字符串并读取与其相关的另一个字符串

我想做一个程序,通过文本文件位于here读取。

一个函数需要一个参数year,并查看该年的文件,并计算当年运动员获得的奖牌并返回总奖牌。

第二个函数以年份,运动员姓氏和名字作为参数并报告运动员奖牌获胜。我知道如何打开文件,以及如何使用for循环和readline命令。

我不知道如何从运动员那里获得奖牌的数量,如果我不直接查找数字本身。如果有人能帮助我改造我的逻辑,将不胜感激。我将发布我的代码以供参考。

def countByYear(year): 
line = 0 
medals = 0 
with open("athletes.txt", encoding= 'utf-8') as f: 
    for line in f: 
     s = f.readline() 
     if s == year: 
      break 
      line2 = f.readline() 
      for line2 in f: 
       if line2.isdigit(): 
        int(line2) 
        medals+=line2 
        break 
    print(medals)   
+4

第一个'break'执行后的代码能执行吗? – 101 2014-09-19 00:04:02

+0

为什么不'如果行==年? – 2014-09-19 00:08:29

+0

我认为行是必要的计数器,我将不得不增加去下一行? – darksoulsfan 2014-09-19 00:09:54

回答

0

如果你想获得幻想,试试这个:

def parse_file(f): 
    competitors = [] 
    for line in f: 
     last, first, year, medals, _ = line.strip(), next(f).strip(), next(f).strip(), next(f).strip(), next(f) 
     competitors.append({"last":last, "first":first, 
          "year":year, "medals":medals}) 
    return competitors 

def count_by_year(year, competitors_dict): 
    # year must be a string 
    year = str(year) 
    return sum(competitor['medals'] for competitor in competitors_dict if competitor['year'] == year) 

def years_by_athlete(firstname, lastname, year, competitors_dict): 
    for competitor in competitors_dict: 
     if competitor['first'] == firstname and \ 
      competitor['last'] == lastname and \ 
      competitor['year'] == year: 
      return competitor['medals'] 
    return None 

if __name__ == "__main__": 
    with open('path/to/your/file', 'r') as f: 
     competitors = parse_file(f) 
    num_medals_by_year = count_by_year(1900, competitors) 
    num_medals_by_athlete = years_by_athlete("DARIA", "PRATT", "1900", competitors) 

记住,文件对象在Python迭代器,所以如果你正在读文件描述如下:

This is the first line 
This is the second line 
This is the third line 
This is the fourth line 
This is a blank line 

那么你可以做

for line in file: 
    firstline = line 
    secondline = next(file) 
    thirdline = next(file) 
    fourthline = next(file) 
    _ = next(file) 

内置的next推进迭代器。代码以5行块的形式贯穿整个文件,将最后一行分配给_(这是“我们不使用这个”的常见Python成语,例如for _ in range(10)做了10次)。然后我们建立一个字典列表,以便稍后参考。这比我们每次查看文件时读取文件要快得多。我们使用strip(除了我们扔掉的线以外,因为......为什么要打扰?)来删除尾随的空格。这包括在每行末尾的换行符(如匹配的文件读取This is the first line准确,你就必须寻找"This is the first line\n"

然后使用内置sum功能,我们使用一台发电机表达式如果'year'值与我们给出的值相符,则给予每个'medals'值。这将展开到:

s = 0 
for competitor in competitors_dict: 
    if competitors_dict['year'] == year: 
     s += competitors_dict['medals'] 
return s 

至于你的第二个功能,你需要的是一系列if条件。通过competitors_dict迭代并确保'first','last''year'字段都与您的参数匹配,然后返回'medals'字段。就这么简单:)

+0

是的..我在声明中意识到我的错误。它需要这些参数并且获得该运动员特别赢得的奖牌。 – darksoulsfan 2014-09-19 00:26:18

+0

@darksoulsfan我明白了。我已经用第二个问题的冗长描述和函数定义编辑了我的代码 – 2014-09-19 00:33:21

相关问题