Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:33:55+00:00 2026-06-02T19:33:55+00:00

I have to parse some numbers from file names that have no common logic.

  • 0

I have to parse some numbers from file names that have no common logic. I want to use the python way of “try and thou shall be forgiven”, or try-except structure. Now I have to add more than two cases. What is the correct way of doing this? I am now thinking either nested try’s or try-except-pass, try-except-pass,… Which one would be better or something else? Factory method perhaps (how?)?

This has to be easily expandable in the future as there will be much more cases.

Below is what I want (does not work because only one exeption per try can exist):

try:
    # first try
    imNo = int(imBN.split('S0001')[-1].replace('.tif',''))
except:
    # second try
    imNo = int(imBN.split('S0001')[-1].replace('.tiff',''))
except:
    # final try
    imNo = int(imBN.split('_0_')[-1].replace('.tif',''))

Edit:

Wow, thanks for the answers, but no pattern matching please. My bad, put “some common logic” at the beginning (now changed to “no common logic”, sorry about that). In the cases above patterns are pretty similar… let me add something completely different to make the point.

except:
    if imBN.find('first') > 0: imNo = 1
    if imBN.find('second') > 0: imNo = 2
    if imBN.find('third') > 0: imNo = 3
    ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:33:56+00:00Added an answer on June 2, 2026 at 7:33 pm

    You can extract the common structure and make a list of possible parameters:

    tries = [
        ('S0001', '.tif'),
        ('S0001', '.tiff'),
        ('_0_', '.tif'),
    ]
    
    for sep, subst in tries:
        num = imBN.split(sep)[-1].replace(subst, '')
        try:
            imNo = int(num)
            break
        except ValueError:
            pass
    else:
        raise ValueError, "String doesn't match any of the possible patterns"
    

    Update in reaction to question edit

    This technique can easily be adapted to arbitrary expressions by making use of lambdas:

    def custom_func(imBN):
        if 'first' in imBN: return 1
        if 'second' in imBN: return 2
    
    tries = [
        lambda: int(imBN.split('S0001')[-1].replace('.tif','')),
        lambda: int(imBN.split('S0001')[-1].replace('.tiff','')),
        lambda: int(imBN.split('_0_')[-1].replace('.tif','')),
        lambda: custom_func(imBN),
    ]
    
    for expr in tries:
        try:
            result = expr()
            break
        except:
            pass
    else:
        # error
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to use the MSHTML library to parse some HTML that I have
I have a file that I open and parse and some of the categories
I have some XML that I want to parse using the lxml method in
I have such files to parse (from scrapping) with Python: some HTML and JS
I have to parse some tables from an ASCII text file. Here's a partial
I have a number of functions that parse data from files, usually returning a
I have some code that reads a file into an array of lines, and
I have looked at GSON to parse some of my JSON objects. However, this
I am trying to parse some XML i have retrieved via e4x in an
I am using SAX to parse some XML. Let's say I have the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.