简体   繁体   中英

Python filtering and selecting from list

I need to make a python function that opens a file, reads in the text and then outputs on the Python GUI any entries that contain dates. Examples for valid dates include "1/30/10", "1/30/2010", "1-30-2010", "01-30-2010", "30.1.2010", "30. 1. 2010", and "2010-01-30." It should have few false positives such as "13010", "01302010", or "30-30-10" as dates.

What I have so far is this

import sys

def main():
    infile = open('testdate.txt', 'r')

    for line in infile:
        words = line.split()
        for date in words:
            if ____ in date:
                print date


    infile.close()

main()

I know that the line.split() function is able to separate all entries in the text file. What I'm unsure about is how to loop through this new list and ONLY take in dates. How would I go about filtering only dates out?

Find out all possible formats and try to parse those. This may help:

>>> from datetime import datetime
>>> possible_fmts = ["%m/%d/%y","%m/%d/%Y","%m-%d-%y","%m-%d-%Y","%d.%m.%Y","%d. %m. %Y","%Y-%m-%d"]
>>> test_text = "1/30/10,1/30/2010,1-30-2010,01-30-2010,30.1.2010,30. 1. 2010,2010-01-30"
>>> for date_token in test_text.split(','):
        for fmt in possible_fmts:
            try:
                print datetime.strptime(date_token, fmt)
                break
            except ValueError, e:
                pass


2010-01-30 00:00:00
2010-01-30 00:00:00
2010-01-30 00:00:00
2010-01-30 00:00:00
2010-01-30 00:00:00
2010-01-30 00:00:00
2010-01-30 00:00:00

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