I have some filenames, and they each have data contained in them at specific locations in the string. I want a function that returns the value of the data specified by field
. The doctest illustrates.
I'd like a nicer, easier-to-understand way to do this.
If possible, but secondarily, I'd like to be able to give the data in different types, according to which field they are, rather than just the string value.
def get_field_posn_list(field):
"""List containing field position indices in filename"""
return({'place': [-11],
'year': range(-10, -7),
'session': [-6]}[field])
def get_field_str(fn, field):
"""Get the string value of field stored in string fn
>>> get_field_str('/path/to/file/Z998-5.asdf', 'session')
'5'
"""
val = ''.join([fn[i] for i in get_field_posn_list(field)])
return val
1 Answer 1
This task is nearly equivalent to another recent question. My advice is that string analysis is usually best done using regular expressions.
import os
import re
def get_field_str(file_path, field):
"""
Get the string value of field stored in file_path
>>> get_field_str('/path/to/file/Z998-5.asdf', 'session')
'5'
"""
pattern = re.compile('(?P<place>.)(?P<year>.{3})-(?P<session>.)\.')
return pattern.search(os.path.basename(file_path)).group(field)
The regex can validate the filename as it works. Feel free to adjust it to match your expectations.
-
\$\begingroup\$ Is
_,
withsplit
preferred toos.path.basename()
? \$\endgroup\$Hatshepsut– Hatshepsut2016年07月30日 06:17:06 +00:00Commented Jul 30, 2016 at 6:17 -
1\$\begingroup\$ No good reason. Brainfart. Corrected in Rev 2. \$\endgroup\$200_success– 200_success2016年07月30日 06:19:06 +00:00Commented Jul 30, 2016 at 6:19