问题
I am trying to read a text file line by line and check the number of occurrence of each group of strings in a line for example. A text file contains these lines (Which varies)
X_0_Gui_Menu_400_Menu_System
X_0_Gui_Menu_400_Menu_System
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_300_Menu_Outputs
X_0_Gui_Menu_300_Menu_Outputs
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
I would like to get an output of
X_0_Gui_Menu_400_Menu_System
2 times
X_0_Gui_Menu_000_Menu_root
3 times
X_0_Gui_Menu_300_Menu_Outputs
2 times
X_0_Gui_Menu_320_Menu_Outputs_SDI
8 times
I saw some forums using count, but I dont know what data will be there in the file to give an input to check, but it is completely random. So I am confused how to implement this. Any guidance would be really helpful.
回答1:
You can achieve this by using the Counter container from the collection module. From the Python documentation: "A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages."
Here is a sample code that does what you are asking for. I used the fact that a file is an iterator to create the Counter object. When you iterate, on a file it yields each line but does not remove the newline character so I used the strip() method to get the output you suggested.
filename = 'test.txt'
filetxt = """\
X_0_Gui_Menu_400_Menu_System
X_0_Gui_Menu_400_Menu_System
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_300_Menu_Outputs
X_0_Gui_Menu_300_Menu_Outputs
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
X_0_Gui_Menu_320_Menu_Outputs_SDI
"""
with open(filename, 'w') as f:
f.write(filetxt)
from collections import Counter
with open(filename, 'r') as f:
c = Counter(f)
# use iteritems() in python 2.7 instead of items
for key, value in c.items():
print(key.strip())
print('{:d} times'.format(value))
回答2:
file = open('test.txt')
fileLines = file.read().split('\n')
list = []
for line in fileLines :
for tup in list:
if tup[0] == line:
list[list.index(tup)][1] = list[list.index(tup)][1] + 1
break
else:
list.append([line, 1])
for s in list:
print(s[0] + ' ' + str(s[1]))
This should read the lines in the file. If the line does not exist in list, then it adds a tuple to the list consisting of the string and the count. If the line does exist, then it just adds 1 to the count in the appropriate tuple.
来源:https://stackoverflow.com/questions/27045146/counting-number-of-times-a-group-of-string-have-occurred-and-print-the-string-an