Python Forum
Update Date based on Time/String
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Update Date based on Time/String
#1
I am absolutely new to Python. While having a bit of experience with perl, ruby, and some other programming languages, I figure it's about time I delve into and learn Python.

Most of my successes in learning tech is to solve an actual problem with it and build on that. So my problem as it sits now is I have a data set. Part of this data is time/date stamps. A sample of the data is below:

Quote:15,07/31/2020 1600,1048,31,0
15,07/31/2020 1700,1086,45,0
15,07/31/2020 1800,1092,42,0
15,07/31/2020 1900,1017,20,1
15,07/31/2020 2000,945,0,28
15,07/31/2020 2100,897,0,43
15,07/31/2020 2200,869,0,55
15,07/31/2020 2300,812,0,72
15,07/31/2020 0000,778,0,82

I need to detect every occurrence of 0000 in the second column (though I don't believe there are any other occurrences of 0000 in any other column so column isolation is probably not necessary) and increase the date by +1 day. So, in the above example, the expected result would be:

Quote:15,07/31/2020 1600,1048,31,0
15,07/31/2020 1700,1086,45,0
15,07/31/2020 1800,1092,42,0
15,07/31/2020 1900,1017,20,1
15,07/31/2020 2000,945,0,28
15,07/31/2020 2100,897,0,43
15,07/31/2020 2200,869,0,55
15,07/31/2020 2300,812,0,72
15,08/01/2020 0000,778,0,82

I suppose I could do this with a combination of awk or perl scripts or maybe even just perl but I think this would be a great way to get my feet wet with Python. Can someone give me some guidance on what is needed to use Python to make this modification?
Reply
#2
There are probably several ways to do this in python. You could simply open the file and process each line, then write it to stdout. Here is one way to process a single line
>>> import datetime as dt
>>> day = dt.timedelta(days=1)
>>> 
>>> s = "15,08/01/2020 0000,778,0,82"
>>> e = s.split(',', 2)
>>> e
['15', '08/01/2020 0000', '778,0,82']
>>> if e[1].endswith('0000'):
...     d = dt.datetime.strptime(e[1].split()[0], '%d/%M/%Y')
...     e[1] = '{:%d/%M/%Y} 0000'.format(d + day)
...     s = ','.join(e)
... 
>>> s
'15,09/01/2020 0000,778,0,82'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 116 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 312 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Create dual folder on different path/drive based on the date agmoraojr 2 377 Jan-21-2024, 10:02 AM
Last Post: snippsat
  Python date format changes to date & time 1418 4 513 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 959 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,160 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Wait til a date and time KatManDEW 2 1,390 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  String concatenation in SQL update statement hammer 3 1,454 Feb-24-2022, 08:00 PM
Last Post: hammer
  Date format and past date check function Turtle 5 4,066 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Print first day of the week as string in date format MyerzzD 2 1,980 Sep-29-2021, 06:43 AM
Last Post: MyerzzD

Forum Jump:

User Panel Messages

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