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
  String to Date format SAF 2 679 Apr-06-2021, 02:09 PM
Last Post: snippsat
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 646 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Naming the file as time and date. BettyTurnips 3 747 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  Confusion about [date]time [formatting] Mark17 0 578 Dec-17-2020, 07:25 PM
Last Post: Mark17
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 594 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  Split gps files based on time (text splitting) dervast 0 467 Nov-09-2020, 09:19 AM
Last Post: dervast
  How to print n days back date at give time Mekala 1 648 Oct-10-2020, 03:35 AM
Last Post: bowlofred
  How to add date and years(integer) to get a date NG0824 4 1,003 Sep-03-2020, 02:25 PM
Last Post: NG0824
  Date and time as filename Herbert58 3 1,117 Aug-08-2020, 10:11 AM
Last Post: Herbert58
  How to update component props every time when a Dash callback returns? sguzunov 0 730 Jul-27-2020, 07:11 AM
Last Post: sguzunov

Forum Jump:

User Panel Messages

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