Reputation: 3
have files with date attached in last part of filename. Like this.
string = 'blablablabla_20210812.jpg'
I extract that data like this.
string[-12:][:-4]
I want to add '-' between year, month and date. This is how I do.
string[-12:][:-4][:4] + '-' + string[-12:][:-4][4:][:2] + '-' + string[-12:][:-4][6:]
In my opinion, it seems like more complicated than reading machine code. Could you guys could enlighten me the ways which are more pragmatic?
Upvotes: 0
Views: 109
Reputation: 89
You can also try like this:
import datetime
print(datetime.datetime.strptime(string[-12:-4],'%Y%m%d').strftime('%Y-%m-%d'))
Output:
'2021-08-12'
Upvotes: 0
Reputation: 86
You can put both indices in one square and use join function:
'-'.join([string[-12:-8], string[-8:-6], string[-6:-4]])
Also, I personally prefer to keep code readable. You can name the variables first:
def extractDataInfo(string):
year, month, day = string[-12:-8], string[-8:-6], string[-6:-4]
return '-'.join([year, month, day])
Upvotes: 0
Reputation: 1054
A simple solution for this question will work only if you know that the file always ends with:
_data.extension
If so, the solution will be:
string = 'blablablabla_20210812.jpg'
s = string.replace("_", ".")
s = s.split(".")
data = s[1]
s[1] = data[:4] + "-" + data[4:6] + "-" + data[6:]
print(s[1]) # OUTPUT: 2021-08-12
# Taking all together:
print(s[0] + "_" + s[1] + "." + s[2]) # OUTPUT: blablablabla_2021-08-12.jpg
Upvotes: 0
Reputation: 709
You can compress the string[-12:][:-4][:4]
to string[-12:-8]
to make it look cleaner. The code will look like this:
string = 'blablablabla_20210812.jpg'
print(string[-12:-8] + '-' + string[-8:-6]+ '-' + string[-6:-4])
# 2021-08-12
Or this if you want the text:
print(string[:-8] + '-' + string[-8:-6]+ '-' + string[-6:-4])
# blablablabla_2021-08-12
Upvotes: 0
Reputation: 18426
You can also replace the values joining the groups with lambda
:
>>> import re
>>> string = 'blablablabla_20210812.jpg'
>>> re.sub('(\d{4})(\d{2})(\d{2})', lambda m: '-'.join(g for g in m.groups()), string)
#output: 'blablablabla_2021-08-12.jpg'
Upvotes: 2
Reputation: 195543
One solution is to use regular expression and re.sub
:
import re
s = "blablablabla_20210812.jpg"
s = re.sub(r"_(\d{4})(\d{2})(\d{2})\.", r"_\1-\2-\3.", s)
print(s)
Prints:
blablablabla_2021-08-12.jpg
Upvotes: 4