Better way / regex to extract values form a dictionary
Ganesh Pal
ganesh1pal at gmail.com
Sat Jul 21 07:37:04 EDT 2018
I have one of the dictionary values in the below format
'/usr/local/ABCD/EDF/ASASAS/GTH/HELLO/MELLO/test04_Failures.log'
'/usr/local/ABCD/EDF/GTH/HEL/OOLO/MELLO/test02_Failures.log'
'/usr/local/ABCD/EDF/GTH/BEL/LO/MELLO/test03_Failures.log'
I need to extract the file name in the path example, say test04_Failure.log
and testcase no i.e test04
Here is my solutions:
gpal-cwerzvd-1# vi filename.py
import re
Common_dict = {}
Common_dict['filename'] =
'/usr/local/ABCD/EDF/GTH/HELLO/MELLO/test04_Failures.log'
def return_filename_test_case(filepath):
if filepath:
filename = re.findall(r'(test\d{1,4}_\S+)', filepath)
if filename:
testcase = re.findall(r'(test\d{1,4})', ''.join(filename))
return filename, testcase
if Common_dict['filename']:
path = Common_dict['filename']
fname, testcase = return_filename_test_case(path)
print fname, testcase
op:
qerzvd-1# python filename.py
['test04_Failures.log']
['test04']
Please suggest how can this code can be optimized further looks messy ,
what would be your one liner or a simple solution to return both test-case
no and filename
I am on Python 2.7 and Linux
Regards,
Ganesh
More information about the Python-list
mailing list