用文件计算文件中的特定单词

时间:2015-03-23 14:57:52

标签: python

我试图使用python计算逗号分隔文件中单词的出现次数。

我有一个包含这样的字符串的文件:

path/to/app1,app1,fail,my@email.com,logfile.log
path/to/app2,app2,success,my@email.com,logfile.log

我想知道多少次"失败"在文件中。

我尝试了几件事,包括

for line in lines:
   if line.split(',') == "fail":
       fails += 1

3 个答案:

答案 0 :(得分:1)

您正在做的是将列表(这是str.split的结果)与字符串fail进行比较,您要做的是检查这些行中是否存在失败:< / p>

for line in lines:
   if "fail" in line.split(','):
       fails += 1

此代码假设fail最多只能出现一次,逗号之间。

正确方法是使用csv module

import csv
fails = 0
with open("logfile.log") as f:
    reader = csv.reader(f)
    for row in reader:
        for item in row:
            if item == "fail":
                fails += 1
print fails

您还可以使用collections.Counter来计算:

import csv
from collections import Counter
counter = Counter()
with open("logfile.log") as f:
    reader = csv.reader(f)
    for row in reader:
        counter.update(row)
print counter['fail']

答案 1 :(得分:0)

不是最佳解决方案,但有效

f = open("C:/Users/Superman/Desktop/krypton.log")
data = f.read()
fail_count =0
no_spaces = data.replace(' ', '')   #can use .strip() also to remove spaces
x = no_spaces.split(',')

for word in x:
  if word == 'fail':
    fail_count += 1
print (fail_count)

答案 2 :(得分:0)

试试这个:

def specific_word_count(text, specific_word):
    return len(text.split(specific_word)) - 1

输入:

specific_word_count('abcdabcdabcd','a')

输出:

3
相关问题