calculate days between several dates in python

2024/11/17 10:03:52

I have a file with a thousand lines. There's 12 different dates in a single row.

I'm looking for two conditions. First: It should analyze row by row. For every row, it should check only for the dates between >=2022-12-1 & <=2024-12-31. Then, it should give me how many there is, BUT, with another specific condition.

for example, if there is: 2023-01-01, 2023-01-02, 2024-01-01, 2019-01-01, It should not give me 3 as an answer, but it should give me 2.

Because, it should calculate how many days there is between every of these dates, and if there is <90 days between one or another, it should "merge" them into one.

so for another example, if I have a set of : '2023-01-01, 2023-10-01, 2024-10-01' I should get 3 as a result.

... I know it's kind of messy, but I would really be glad to get some help.

EDIT:

I made a mistake in my original post. There is indeed 12 dates in every row. But they should be grouped by 2. So the first one with the second, the third one with the fourth, the fifth with the sixth and so on..

So let's take my first example :

2023-01-01, 2023-01-02, 2024-01-01, 2019-01-01.

It should calculate the difference between each one of these groups, so 2023-01-01 with 2023-01-02, AND, 2024-01-01 with 2019-01-01. Anytime there is 2 dates that are <90 days apart, it should count them as 1. If they are >90 days apart, it should count them as 2. So If I calculate the difference between 2023-01-01 and 2023-01-02, it gives me <90 days. So it's 1. Then i calculate 2024-01-01 with 2019-01-01, but since 2019-01-01 doesn't fit the first condtition, it should be ignored and count 2024-01-01 as one. Finaly it should add the first answer to the second, and give me the final answer, which is 2.

If instead 2019-01-01, I had 2024-05-01, I should give me 2 as the answer for that group since 2024-01-01 - 2024-05-01 = >90 days. Then finaly I would add 1 + 2 and the final answer would be 3.

Answer

One way to solve it would be the one below (Keep in my that this is a minimum example, describing the logic):

from datetime import datetimeline = "2023-01-01, 2023-01-02, 2024-01-01, 2019-01-01"# Split and convert strings to datetime objects
dates = list(datetime.strptime(elem, '%Y-%m-%d') for elem in line.split(', '))total = 0
# Read pairs of dates and decide whether they should be merged or not.
for i in range(0, len(dates), 2):delta = dates[i+1] - dates[i]if delta.days > 90:total += 2else:total += 1
print(total)

This will return:

2

You can also create a function for that and use it for each row:

from datetime import datetimedef getTotal(line):dates = list(datetime.strptime(elem, '%Y-%m-%d') for elem in line.split(', '))total = 0for i in range(0, len(dates), 2):delta = dates[i+1] - dates[i]if delta.days > 90:total += 2else:total += 1return totalline = "2023-01-01, 2023-01-02, 2024-01-01, 2019-01-01"
anotherLine = "2023-01-01, 2023-01-02, 2024-01-01, 2024-05-01"print(getTotal(line))
print(getTotal(anotherLine))

This will return:

2
3
https://en.xdnf.cn/q/120217.html

Related Q&A

Appeding different list values to dictionary in python

I have three lists containing different pattern of values. This should append specific values only inside a single dictionary based on some if condition.I have tried the following way to do so but i go…

Split only part of list in python

I have a list[Paris, 458 boulevard Saint-Germain, Marseille, 29 rue Camille Desmoulins, Marseille, 1 chemin des Aubagnens]i want split after keyword "boulevard, rue, chemin" like in output[Sa…

How to find the index of the element in a list that first appears in another given list?

a = [3, 4, 2, 1, 7, 6, 5] b = [4, 6]The answer should be 1. Because in a, 4 appears first in list b, and its index is 1.The question is that is there any fast code in python to achieve this?PS: Actual…

How to yield fragment URLs in scrapy using Selenium?

from my poor knowledge about webscraping Ive come about to find a very complex issue for me, that I will try to explain the best I can (hence Im opened to suggestions or edits in my post).I started usi…

Django Database Migration

Hi have a django project a full project now I want to migrate to mysql from the default Sqlite3 which is the default database. I am on a Mac OS and I dont know how to achieve this process. Any one wit…

Search engine using python for bookmarked sites [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

How to extract only particular set of structs from a file between braces in python

a. Have a scenario, where in my function reads in a file which contains list of c-structures as shown below, reads the file and extracts all the information between { } braces for each structure and st…

Selection of Face of a STL by Face Normal value Threshold

I want to write a script in Python which can generate facegroups in a STL as per the Face Normal value condition. For example, Provided is the snap of Stl, Different colour signifies the face group con…

Python , Changing a font size of a string variable

I have a variable that gets sent to a email as text but the text is all pretty much a standard size with everything the same. I would like to add some emphasis to it as well as make it bigger and make …

Python http.server command gives Syntax Error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…