Reputation: 1470
I have a dict like this:
SHOPS_AND_ORDERNUM = {
'Shop - Produce - 20180212.xlsx': 1334,
'Shop - Organic - 20180223.xlsx': 8893,
'Shop - Fresh - 20180226.xlsx': 5557,
'Shop - Dairy - 20180227.xlsx': 3870
}
I want to extract the dates from the dict above into the form: DD-MM-YYYY
I am new to regular expressions, and my attempts keep failing.
I have started something like this:
for i, j in DATA_FILES_AND_SO.items():
m = re.search(some_logic, i)
if m:
found = m.group(1)
Any help would be much appreciated!
Upvotes: 0
Views: 1896
Reputation: 7238
If the format does not change, you can use this (no need of RegEx):
SHOPS_AND_ORDERNUM = {
'Shop - Produce - 20180212.xlsx': 1334,
'Shop - Organic - 20180223.xlsx': 8893,
'Shop - Fresh - 20180226.xlsx': 5557,
'Shop - Dairy - 20180227.xlsx': 3870
}
for item in SHOPS_AND_ORDERNUM:
date = item.split('.xlsx')[0][-8:]
print(date)
Output:
20180212
20180223
20180226
20180227
Now, to get the date in the format you want, you can use the datetime
module, like this:
for item in SHOPS_AND_ORDERNUM:
date = datetime.datetime.strptime(item.split('.xlsx')[0][-8:], '%Y%m%d').strftime('%d-%m-%Y')
print(date)
Output:
12-02-2018
23-02-2018
26-02-2018
27-02-2018
Upvotes: 1
Reputation: 1801
The basic regex you're looking for is ([0-9]+)(?=.)\g. You can play around with it on https://regex101.com/.
For date string conversion, you can use the Carbon library, such as
$newDateString = \Carbon::parse('20180212')->format('DD-MM-YYYY');
See also the Carbon Docs.
Upvotes: 1
Reputation: 71451
You can use regex:
import re
SHOPS_AND_ORDERNUM = {
'Shop - Produce - 20180212.xlsx': 1334,
'Shop - Organic - 20180223.xlsx': 8893,
'Shop - Fresh - 20180226.xlsx': 5557,
'Shop - Dairy - 20180227.xlsx': 3870
}
new_data = {(lambda x:(x[7:], x[4:6], x[:4]))(re.findall('\d+', a)[0]):b for a, b in SHOPS_AND_ORDERNUM.items()}
Output:
{('2', '02', '2018'): 1334, ('3', '02', '2018'): 8893, ('6', '02', '2018'): 5557, ('7', '02', '2018'): 3870}
Or, instead of tuples:
new_data = {'{}-{}-{}'.format(*a[::-1]):b for a, b in new_data.items()}
Output:
{'2018-02-2': 1334, '2018-02-7': 3870, '2018-02-3': 8893, '2018-02-6': 5557}
Upvotes: 3
Reputation: 82765
You can use the datetime
module to get your required date format
Ex:
# -*- coding: utf-8 -*-
import datetime
SHOPS_AND_ORDERNUM = {
'Shop - Produce - 20180212.xlsx': 1334,
'Shop - Organic - 20180223.xlsx': 8893,
'Shop - Fresh - 20180226.xlsx': 5557,
'Shop - Dairy - 20180227.xlsx': 3870
}
for k,v in SHOPS_AND_ORDERNUM.items():
print datetime.datetime.strptime(k.split("-")[-1].rstrip(".xlsx").strip(), "%Y%m%d" ).strftime("%d-%m-%Y")
Output:
27-02-2018
26-02-2018
23-02-2018
12-02-2018
MoreInfo
k.split("-")[-1].rstrip(".xlsx").strip()
#to get the date string from key. ex: 20180212
datetime.datetime.strptime
#to convert datetime to your required format. "%d-%m-%Y"
Upvotes: 3
Reputation: 3244
import datetime
dates = []
for i, j in DATA_FILES_AND_SO.items():
date = i[-13:-5]
dates.append(datetime.datetime.strptime(date, '%Y%m%d'))
Upvotes: 3