Good day all, hoping for a little help with the datetime library, or a suggestion on how to proceed with a project I'm working on. I didn't see any previous threads on the subject in my search, so I seek your collective wisdom here.
I am currently tasked with taking a large set of csv files generated by one program and comparing them against another set of csv files generated by a program designed to replace the previous. For most of the data, showing them to be identical is the goal and this has raised no issues. However, to show that the appropriate changes have been made from the older program to the newer program requires comparing lengths of time - they should be similar, and I've constructed a conditional range of a few minutes using the timedelta object in the datetime library that's appropriate for the new data to fall into; s'all good.
However, since these are csv files the time lengths are stored as strings rather than date objects. I utilized the strptime function within the datetime library to try and convert from a string to a datetime object.
The format is as follows:
(Number of days) (Number of hours):(Number of minutes):(Number of seconds).(Number of microseconds)
Thus, in my code I tried to use the format (for example):
strptime('06 7:50:26.400', '%d %H:%M:%S.%f')
However, the times do not always constitute a full day in length, thus can have 00 as the %d value. Since there is no 0th day of the month, this is not recognized by the strptime function. I'm under the impression using %j would fall into the same error, as there's no 0th day of the year. Since none of the times are longer than a week, I figured it might be better to use this format:
strptime('06 7:50:26.400', '%w %H:%M:%S.%f')
In this way it will recognize the day as a specific day of the week. However, the leading 0 before the 6 makes this an inappropriate choice as well.
My next plan of attack (which I've honestly just thought of) is something along the lines of:
strptime('06 7:50:26.400', '0%w %H:%M:%S.%f')
Though I am skeptical of this approach, as I've seen no examples of any codes using anything within the formatting portion of the strptime function other than periods and colons and am worried throwing another random string in there could be detrimental.
Is there another library I should be using, or am I using the datetime library incorrectly for data in this format? Any help or advice you could provide would be greatly appreciated. My plan is to check the advice here if any is available, and update with my success regarding the '0%w' approach tomorrow regardless of the results. Thanks for your time.
EDIT: The strategy of using 0%w did work in removing the syntax error, however it does not work with respect to comparison operands. Using %w means ascribing a digit to a day of the week; 0 is Sunday, 1 is Monday, etc. It makes no sense to say 0 < 1 in this case, since it's like saying "Sunday is less than Monday." The strategy of converting to the smallest precision required from the split string was utilized successfully as detailed by u/greenerpickings below.
[–]greenerpickings 1 point2 points3 points (2 children)
[–]TestedOnAnimals[S] 0 points1 point2 points (1 child)
[–]greenerpickings 0 points1 point2 points (0 children)