Convert dates in Python

17 views
Skip to first unread message

אורי

unread,
Jul 29, 2022, 6:13:56 AM7/29/22
to pywe...@googlegroups.com
Hi,

I have a text file with lots of text and dates in the format dd/mm/yyyy, with years ranging from 1970 to 2022. I want to read this file and convert the dates to yyyy-mm-dd format, while keeping all the text as it is. Do you know how to do it with Python? Or I don't mind using another tool (such as awk, sed) but as long as the rest of the file will not be affected.

Optionally, I want to search also for dates without leading zeros in day or month, and convert them too. But first I want to display them (I'm not sure if there are such dates).

It's important not to convert other strings, so if the "year" is not from 1970 to 2022, don't convert the string.

Thanks,

Omer Zak

unread,
Jul 29, 2022, 6:33:29 AM7/29/22
to pywe...@googlegroups.com
(NOTE: I am Israeli, so if you support BDS, don't read the following!)

Check the function re.sub, using a callable as 2nd parameter (called
'repl' in help(re.sub)).
--
There is no IGLU Cabal because
My own blog is at https://tddpirate.zak.co.il/

My opinions, as expressed in this E-mail message, are mine alone.
They do not represent the official policy of any organization with
which I may be affiliated in any way.
WARNING TO SPAMMERS: at https://www.zak.co.il/spamwarning.html


Reply all
Reply to author
Forward
0 new messages