Newbie ICT IS Newbie ICT IS - 1 month ago 7
Python Question

How to group set of time under distinct date from csv file

hi i have just gotten a set of time and date data from csv file using regex:

datePattern = re.compile(r"(\d+/\d+/\d+\s+\d+:\d+)")
for i, line in enumerate(open('sample_data.csv')):
for match in re.finditer(datePattern, line):
date.append(match.groups());


the output is [('30/06/2016 08:30',), ('20/07/2016 09:30',),
('30/06/2016 07:30',)

How do i turn it into useful information such as listing all the time under the same date such as maybe [('30/06/2016 08:30',07.30),]

Answer

Try this regex:

r"(\d+/\d+/\d+)\s+(\d+:\d+)"

Python code follows, I have used dictionary of lists for such grouping

import re

datePattern = re.compile(r"(\d+/\d+/\d+)\s+(\d+:\d+)")  
dateDict =dict()    

for i, line in enumerate(open('sample_data.csv')):
    for match in re.finditer(datePattern,line):
        if match.group(1) in dateDict:
            dateDict[match.group(1)].append(match.group(2))
        else:
            dateDict[match.group(1)] = [match.group(2),]        
print(dateDict)         

It will output as follows:

{'10/10/1990': ['12:20', '11:20'], '10/10/1991': ['16:20', '16:20']}

Tested with python 3+