'parse.parse, specify expressions width
I am using parse.parse to find a file according to how its name matches with a given pattern. Is it possible to define specific expressions width inside the pattern to add more conditions for the file research ?
Let's suppose the following code :
from parse import parse
patterns = ['file_{city:3w}_{date:6w}', 'file_another_one_{city:3w}_{date:6w}']
def find_and_display_pattern(filename):
print('### searching for file {} ###'.format(filename))
for pattern in patterns:
parse_result = parse(pattern, filename)
if not parse is None:
print('{} pattern found for file {}'.format(pattern, filename))
print('result :')
print(parse_result)
return
find_and_display_pattern('file_PRS_02182022')
find_and_display_pattern('file_another_one_PRS_02182022')
I get the following output :
### searching for file file_PRS_02182022 ###
file_{city:3w}_{date:6w} pattern found for file file_PRS_02182022
result :
<Result () {'city': 'PRS', 'date': '02182022'}>
### searching for file file_another_one_PARIS_02182022 ###
file_{city:3w}_{date:6w} pattern found for file file_another_one_PRS_02182022
result :
<Result () {'city': 'another_one_PRS', 'date': '02182022'}>
My issue for the file 'file_another_one_PRS_02182022' is that I except to retrieve the second pattern : 'file_another_one_{city:3w}_{date:6w}' with the specific expressions width (3 characters for city and 6 characters for date) Which would give the following output :
### searching for file file_another_one_PRS_02182022 ###
file_another_one_{city:3w}_{date:6w} pattern found for file file_another_one_PRS_02182022
result :
<Result () {'city': 'PRS', 'date': '02182022'}>
Can parse.parse handle this ? If not is there any other way to proceed this ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
