如何计算某一行的数量?

时间:2014-09-20 18:27:40

标签: python file sum

我正在从textfile中读取文件。

文字档案:

Mike
2006
1

Dylan
2007
1

Ben
2007
1

English
2008
1

James
2008
0

我如何计算每年的总数?

例如,2006年只有1个,2007年只有2个,2008年只有1个。

代码:

f = open("file.txt")
    for line in f:
        readFile = line.strip();
        print(readFile)
    f.close()

有什么建议吗?

3 个答案:

答案 0 :(得分:0)

你可以用这个解决:

temp=[]
def count_year_num(year):
    f = open("test.txt")
    lines=''.join(list(f))
    newgrouping=lines.split('\n\n') # grouping the content of file result is like this : ['Mike\n2006\n1', 'Dylan\n2007\n1', 'Ben\n2007\n1', 'English\n2008\n1', 'James\n2008\n0\n']

    newgrouping= [i.split('\n') for i in newgrouping] #splite the grouped content :[['Mike', '2006', '1'], ['Dylan', '2007', '1'], ['Ben', '2007', '1'], ['English', '2008', '1'], ['James', '2008', '0', '']]

    for i in newgrouping:
        if year in i:
            temp.append(int(i[2])) # append the numbers to a temp list
    return sum(temp) # find the sum of number

演示:

>>>count_year_num('2008')
1
>>>count_year_num('2007')
2

答案 1 :(得分:0)

>>> a=defaultdict(int)
>>> with open('file','r') as f:
...     while True:
...             if not f.readline():
...                     break
...             key=f.readline().strip()
...             value=f.readline().strip()
...             a[key] = a[key] + int(value)
...             if not f.readline():
...                     break

>>> dict(a)
{'2008': 1, '2006': 1, '2007': 2}

答案 2 :(得分:0)

一种方法是使用正则表达式来提取年份。这仅适用于文本中数字数字的年份。 首先,将整个文本读入变量:

with open("file.txt") as f:
    text = f.read()

接下来使用正则表达式提取所有年份。这将返回匹配列表:

lst = re.findall("\d{4}", text)

最后一步是使用years作为键来填充字典,值是出现的次数:

d = {}
for year in lst:
    if year in d:
        d[year] +=1
    else:
        d[year] = 1

字典' d'现在可以查询文本中出现的年份数:

print d['2007'] # prints 2
相关问题