简体   繁体   中英

Python - Looking to remove remaining string using slice

Looking to remove the rest of a date input in the folllowing format: 2018-10-03 11:46:00. I want to remove the time portion. So far my code reads:

out1.due_date = out1.due_date[10:]

I am using a program called Data 360, and the out1 is what is used to process the records. However, I am a bit puzzled on how I could remove everything after the yyyy-mm-dd input.

Thanks!

Use split function to achieve this, the method splits a string into a list.You can specify the separator, default separator is a whitespace. So out1.due_date.split() would return list ['2018-10-03','11:46:00']. Since interested in date, use zero as the index.

out1.due_date= out1.due_date.split()[0]  

Using fromisoformat to parse the string to a datetime object and extracting date part out of it, by calling date() on it.

from datetime import date, datetime

print(datetime.fromisoformat(out1.due_date).date())

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM