简体   繁体   中英

Validate date string using Python

I have a python dict which has three keys 'image1', 'image2' and 'similarity'. The dicts have been generated from duplicate-image-finder , as such the image1&2 dict values comprise of a folder location string, which looks similar to this.

../../Amazon Drive/Tony Leaving Do Nov 2018/IMG_20181108_185110.jpg

Example of folder structure:

文件夹

I am using datetime.strptime to test whether folders end with <month> <year> as I am likely to want to keep images in those folders. My plan is to duplicate images that exist in folders containing 'ToSort' without having to manually review every duplicate. The following works but feels a bit clunky. Is there a better way?

My validate function


def validate_date(data):
    try:
        datetime.strptime(data[0] + ' ' + data[1], '%b %Y')
    except ValueError:
        pass
    else:
        return True

    try:
        datetime.strptime(data[0] + ' ' + data[1], '%B %Y')
    except ValueError:
        pass
    else:
        return True

    return False

Function calling


if images_list["similarity"] == 100:

    # Get date from folder name
    f1_date = images_list["image1"].split("/")[-2].split(" ")[-2:]

    img1_folder_valid = validate_date(f1_date)

    if img1_folder_valid is True and "ToSort" or "WhatsApp - Various" in images_list["image2"]:
        print("Adding {} to deletion list\n".format(images_list["image2"]))
        to_delete.add(images_list["image2"])


After this, I open each file using the PIL library for manual comparison. Then using a while True loop ask which file to keep. Rather than doing this, does a good picker function exist that can display a thumbnail and allow for an image to be selected?

You can use glob to list all the folders behind a path. This avoids checking twice the same folder.

import glob

folders = glob.glob('../../Amazon Drive/**/',recursive=True) # Python3 accepts the recursive arg

And then you can check the name.

for folder in folders: # and other conditions ...
    folder_is_valid = validate_date(folder)

Then, a much more readable way to implement the function is to use the path as input.

def validate_date(folder):
    folder_name = folder.split('/')[-2]
    ...

Edit The validate function can be restructured a little but unless you can assure that the correct format to strptime the try/catch blocs are necessary:

def validate_date(data):
    try:
        datetime.strptime(data[0] + ' ' + data[1], '%b %Y')
        return True
    except ValueError:
        pass

    try:
        datetime.strptime(data[0] + ' ' + data[1], '%B %Y')
        return True
    except ValueError:
        pass        

    return False

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