I want to remove special character '-' from date format in python. I have retrieved maximum date from a database column. Here is my small code:
def max_date():
Max_Date= hive_select('SELECT MAX (t_date) FROM ovisto.ovisto_log')
value = Max_Date[0]
print value
Here is output:
{0: '2017-02-21', '_c0': '2017-02-21'}
I want only numbers without special character '-' from output. so, I am expecting this answer '20170221'
I have tried in different ways but could not get proper answer. How can I get in a simple way? Thanks for your time.
just rebuild a new dictionary using dict comprehension, iterating on the original dictionary, and stripping the unwanted characters from values using str.replace
d = {0: '2017-02-21', '_c0': '2017-02-21'}
new_d = {k:v.replace("-","") for k,v in d.items()}
print(new_d)
result:
{0: '20170221', '_c0': '20170221'}
if you only want to keep the values and drop the duplicates (and the order too :), use a set comprehension with the values instead:
s = {v.replace("-","") for _,v in d.items()}
You can try strptime:
value = Max_Date[0]
new_val= datetime.datetime.strptime( str( value ), '%Y%m%d').strftime('%m/%d/%y')
I found it here: How to convert a date string to different format
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.