简体   繁体   中英

python regular expression count and print

I have following file text.text:-

08-JUN-2013 05:04:02
08-JUN-2013 05:04:02
08-JUN-2013 05:04:02
08-JUN-2013 05:04:03
08-JUN-2013 05:04:03
08-JUN-2013 05:05:23
08-JUN-2013 05:05:23
08-JUN-2013 05:05:23
08-JUN-2013 05:05:23

I want to print something like this using Python:-

08-JUN-2013 05:04:02 ------3
08-JUN-2013 05:04:03-------2
08-JUN-2013 05:05:23-------4

Here is what i did on this and i am newbie here :-

"
import re
pattern = re.compile("\\d+-\\w+-\\d+\\s\\d+:\\d+:\\d+")
open_file=open('mytest.txt')
for line in open_file:
four_letter_words = pattern.findall(line)
fp_len = len(pattern.findall(line))
for word in four_letter_words:
print four_letter_words , fp_len
"

No need for regex .
you can use counter from collections modul, as described here SO already answered

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM