-3

I have python code where I have an input parameter. The other application is going to pass the parameter. Here's the requirement. Parameter will be passed like this.

  1. abc_201901.txt
  2. abc.201901.txt
  3. abcde_201901.txt

From the above parameter, I need to extract "abc_" or "abc". or "abcded_", and then go to the directory and search for the files.

I am having hard time to find the function which can do this.

martineau
  • 119,623
  • 25
  • 170
  • 301
user138770
  • 15
  • 5
  • 1
    Describe also the logic. Do you need all the characters before the first occurrence of a number? – fferri Oct 14 '19 at 15:42
  • 1
    ...if so, this will do the "extract" part: `''.join(itertools.takewhile(lambda c: not c.isdigit(), param))` – fferri Oct 14 '19 at 15:45
  • 1
    You can also use the [split()](https://www.w3schools.com/python/ref_string_split.asp) function along with a [regex](https://www.w3schools.com/python/python_regex.asp), to attain your objective. – Rahul Soni Oct 14 '19 at 15:46
  • 2
    What directory and what files? What have you already tried? Please read [ask] then [edit] to clarify. – wjandrea Oct 14 '19 at 15:48
  • I need to read until the . or _. For example if parameter is passed like this. Abc_201901.txt then I need to read Abc_ , if parameter is passed Abcd.201901 then I need to read until Abcd. I don't want to write lot of if statements, was looking for some function which can do with one line code – user138770 Oct 14 '19 at 16:11

2 Answers2

1

if you use only "_" and ".", you can use re.search:

import re

a = "test.123.txt"
regex = r"([0-9].*)"

m = re.search(regex, a) # match from any digit to end
m.group(1) # ->'123.txt'

Good luck

--- Correction ---

It's better to take all characters except number with the regex:

import re

a = "test.123.txt"
regex = r"([^0-9]*)"

m = re.search(regex, a) # match all characters except number
m.group(1) # -> 'abc_'
  • Sounds like good advice generally, but I _think_ the OP would want `'test'` to be the result, so would need a different regex to do that. – martineau Oct 14 '19 at 16:15
  • Thank you. In your example you have hardcoded .*, how do I check for multiple characters like "." ,"_", "-" – user138770 Oct 14 '19 at 16:16
1

A possible solution with slicing

def extract_prefix(filename):
    # list of separators
    separators = ['.', '_', '-']
    for sep in separators:
        # rid off file extension
        if sep in filename[:-4]:
            # slicing, including sep (+1)
            return filename[:filename.find(sep) + 1]
    return None

a = "test_123.txt"
prefix = extract_prefix(a)
print(prefix)
jcurwen31
  • 11
  • 1