Date time regular expression help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crashhold
    New Member
    • Nov 2012
    • 1

    #1

    Date time regular expression help

    Hello Developers,

    I am a beginner in python and need help with writing a regular expression for date and time to be fetched from some html documents. In the following code I am walking through the html files in a folder called event and printing the headings with h1 tag using beautifulsoup. These html pages also contains different formats of date and time. I want to fetch and display this information as well. Different formats of date in these html documents are:

    21 - 27 Nov 2012
    1 Dec 2012
    30 Nov - 2 Dec 2012
    26 Nov 2012

    Can someone help me out with fetching these formats from these html documents ?
    Here is my code for walking through the files and fetching h1 from those html files:


    Code:

    Code:
     
        import re
        import os
        from bs4 import BeautifulSoup
        
        for subdir, dirs, files in os.walk("/home/himanshu/event/"):
            for fle in files:
                path = os.path.join(subdir, fle)    
                soup = BeautifulSoup(open(path))
                
                print (soup.h1.string)
               
                #Date and Time detection
    Last edited by zmbd; Nov 26 '12, 02:17 PM. Reason: [Z{Please use the <CODE/> format button in the toolbar to format posted Code/SQL}]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You can compile a list of struct_time objects using a combination of re and time modules.
    Code:
    import time
    import re
    
    patt = re.compile(r"^(\d{1,2}) - (.+)|^(\d{1,2} [a-zA-Z]{3}) - (.+)")
    
    date_format = "%d %b %Y"
    date_format1 = "%d %m %Y"
    
    dates = ["21 - 27 Nov 2012", "1 Dec 2012", "30 Nov - 2 Dec 2012", "26 Nov 2012"]
    
    output = []
    
    for d in dates:
        try:
            m = patt.match(d)
            if m:
                if m.groups()[0]:
                    end_date = time.strptime(m.groups()[1], date_format)
                    output.append((time.strptime("%s %s %s" % (m.groups()[0],
                                                               end_date.tm_mon,
                                                               end_date.tm_year),
                                                 date_format1), end_date))
                elif m.groups()[2]:
                    end_date = time.strptime(m.groups()[3], date_format)
                    output.append((time.strptime("%s %s" % (m.groups()[2],
                                                            end_date.tm_year),
                                                 date_format), end_date))
            else:
                output.append(time.strptime(d, date_format))
        except ValueError, e:
            print "ValueError:", e
            
    for item in output:
        if isinstance(item, tuple):
            print "  From:", item[0]
            print "    To:", item[1]
        else:
            print item

    Comment

    Working...