-4

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
"

  • 1
    This has nothing to do with regular expressions. Read up on dictionaries instead (or also, because you'll need regexes someday :-) ). – blm Nov 25 '15 at 18:41
  • 1
    You should try to have the ***3** what's* in your question: **What** do you want to do? **What** have you tried? **What** is wrong? – d0nut Nov 25 '15 at 18:43
  • Thanks for answer on this . As i am learning Python and newbie i will read dictionaries . – coolwhitepaper Nov 25 '15 at 19:20

1 Answers1

1

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

Community
  • 1
  • 1
Enkelli
  • 305
  • 5
  • 18
  • Thanks I tried this its not working . As they are dates and i want to group by Seconds and then print the result. – coolwhitepaper Nov 25 '15 at 22:27
  • have you used sort method? I suppose you have used counter, then result is dictionary, so try `sorted(result)` – Enkelli Nov 26 '15 at 07:03