Cyber_Tron
Cyber_Tron

Reputation: 299

Extract a name substring from a filename and store it in a variable in Python

I have a tar file whose name I am successfully able to read and store in a variable,

tarname = 'esarchive--Mona-AB-Test226-8037affd-06d1-4c61-a91f-816ec9cb825f-05222017-4.tar'

But how do I extract just "Mona" from this file name and store it in a variable?

(The filename structure for the tar file will be same as above for all tar files with the name occuring after "es-archive--{Name}-AB" , so a solution which returns any name obeying this format)

Thanks!

Upvotes: 2

Views: 4370

Answers (3)

wim
wim

Reputation: 363616

parse module is good for this kind of stuff. You may think of it as the inverse of str.format.

from parse import parse
pattern = 'esarchive--{Name}-AB-{otherstuff}.tar'
result = parse(pattern, tarname)

Demo:

>>> result = parse(pattern, tarname)
>>> result['Name']
'Mona'
>>> result.named
{'Name': 'Mona',
 'otherstuff': 'Test226-8037affd-06d1-4c61-a91f-816ec9cb825f-05222017-4'}

Upvotes: 11

steliosbl
steliosbl

Reputation: 8921

Easiest way I can think of:

  1. Split the filename on the - character.
  2. Get the 3rd item from the resulting list (index 2).

In code:

filename.split('-')[2]

Simple one-liner. This is of course working off your example. I would need more sample filenames to account for possible variations and know for certain if this will always work.

Upvotes: 4

Kevin
Kevin

Reputation: 76264

>>> import re
>>> tarname = "esarchive--Mona-AB-Test226-8037affd-06d1-4c61-a91f-816ec9cb825f-05222017-4.tar"
>>> s = re.match("esarchive--(\w+)-AB", tarname).group(1)
>>> s
'Mona'

Upvotes: 3

Related Questions