Search code examples
pythonfor-loopif-statementvalueerror

How to overcome a ValueError when working with multiple if conditions?


I'm trying to make a script that can identify if a folder name is a project or not. To do this I want to use multiple if conditions. But I struggle with the resulting ValueError that comes from checking, for example, the first letter of a folder name and if it is a Number. If it's a String i want to skip the folder and make it check the next one. Thank you all in advance for your help. Cheers, Benn

I tried While and except ValueError: but haven't been successful with it.

# Correct name to a project "YYMM_ProjectName" = "1908_Sample_Project"

projectnames = ['190511_Waldfee', 'Mountain_Shooting_Test', '1806_Coffe_Prime_Now', '180410_Fotos', '191110', '1901_Rollercoaster_Vision_Ride', 'Musicvideo_LA', '1_Project_Win', '19_Wrong_Project', '1903_di_2', '1907_DL_2', '3401_CAR_Wagon']

# Check conditions

for projectname in projectnames:
    if int(str(projectname[0])) < 3 and int(projectname[1]) > 5 and ((int(projectname[2]) * 10) + int(projectname[3])) <= 12 and str(projectname[4]) == "_" and projectname[5].isupper():
        print('Real Project')
        print('%s is a real Project' % projectname)
        # print("Skipped Folders")

ValueError: invalid literal for int() with base 10: 'E'


Solution

  • From what I understand from all the ifs...you may actually be better off using a regex match. You're parsing through each character, and expecting each individual one to be within a very limited character range.

    I haven't tested this pattern string, so it may be incorrect or need to be tweaked for your needs.

    import re
    
    projectnames = ['1911_Waldfee', "1908_Project_Test", "1912_WinterProject", "1702_Stockfootage", "1805_Branded_Content"]
    
    p = ''.join(["^", # Start of string being matched
                 "[0-2]", # First character a number 0 through 2 (less than 3)
                 "[6-9]", # Second character a number 6 through 9 (single digit greater than 5)
                 "(0(?=[0-9])|1(?=[0-2]))", # (lookahead) A 0 followed only by any number 0 through 9 **OR** A 1 followed only by any number 0 through 2
                 "((?<=0)[1-9]|(?<=1)[0-2])", # (lookbehind) Match 1-9 if the preceding character was a 0, match 0-2 if the preceding was a 1
                 "_", # Next char is a "_"
                 "[A-Z]", #Next char (only) is an upper A through Z
                  ".*$" # Match anything until end of string 
        ])
    
    for projectname in projectnames:
        if re.match(p, projectname):
            #print('Real Project')
            print('%s is a real Project' % projectname)
            # print("Skipped Folders")
    

    EDIT: ========================

    You can step-by-step test the pattern using the following...

    projectname = "2612_UPPER"
    p = "^[0-2].*$" # The first character is between 0 through 2, and anything else afterwards
    if re.match(p, projectname): print(projectname) 
    # If you get a print, the first character match is right.
    # Now do the next
    p = "^[0-2][6-9].*$" # The first character is between 0 through 2, the second between 6 and 9, and anything else afterwards
    if re.match(p, projectname): print(projectname) 
    # If you get a print, the first and second character match is right.
    # continue with the third, fourth, etc.