Python Forum
Formatting a date time string read from a csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting a date time string read from a csv file
#1
L.S.,

I'm rather new to python-programming. I'm trying to remove the time info from a date time string read from a csv-file. My impression is that to achieve this, I have to use the strptime() function.

The test-script i'm using:
from datetime import datetime

my_datetime ='2023-06-15 07:26:50'
my_date = datetime.strptime(my_datetime, '%Y-%m-%d' )
print(my_date)
The error message I get in line 4:
Error:
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/_strptime.py", line 352, in _strptime raise ValueError("unconverted data remains: %s" % ValueError: unconverted data remains: 07:26:50
What I want to achieve is that 'my_date' contains the string '2023-06-15' without the time info of 'my_datetime'.

Who can help me with this issue?
Thanks in advance
Reply
#2
Why not simply use the .date() method?

from datetime import datetime

today = datetime.today()
date = today.date()
print(today, type(today))
print(date, type(date))
date = str(date)
print(date, type(date))
Output:
2023-06-19 13:01:48.271420 <class 'datetime.datetime'> 2023-06-19 <class 'datetime.date'> 2023-06-19 <class 'str'>
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
This date string is just what is read from the csv file:

my_datetime ='2023-06-15 07:26:50'.

I cannot change that and choose my 'own' format, I just have to remove the time info form de string.
Reply
#4
Okay. In that case, you can use string slicing:

my_datetime = '2023-06-15 07:26:50'

my_date = my_datetime[0:10]

print(my_date)
Output:
2023-06-15
DosAtPython likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
>>> from datetime import datetime
my_datetime ='2023-06-15 07:26:50'
>>> my_date = datetime.strptime(my_datetime, '%Y-%m-%d %H:%M:%S').date()
>>> my_date
datetime.date(2023, 6, 15)
>>> my_date.strftime('%Y-%m-%d')
'2023-06-15'
Also if you only want the string
>>> my_datetime ='2023-06-15 07:26:50'
>>> my_datetime.split()[0]
'2023-06-15'
buran likes this post
Reply
#6
(Jun-19-2023, 12:14 PM)rob101 Wrote: Okay. In that case, you can use string slicing:

my_datetime = '2023-06-15 07:26:50'

my_date = my_datetime[0:10]

print(my_date)
Output:
2023-06-15

Thanks. Works.
rob101 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 251 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 381 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 624 Jan-20-2024, 04:45 AM
Last Post: 1418
  Recommended way to read/create PDF file? Winfried 3 2,902 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,475 Nov-09-2023, 10:56 AM
Last Post: mg24
  Need to replace a string with a file (HTML file) tester_V 1 777 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 1,059 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  read file txt on my pc to telegram bot api Tupa 0 1,135 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,127 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  How do I read and write a binary file in Python? blackears 6 6,715 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020