简体   繁体   English

计算python中几个日期之间的天数

[英]calculate days between several dates in python

I have a file with a thousand lines.我有一个一千行的文件。 There's 12 different dates in a single row.一行中有 12 个不同的日期。

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 .对于每一行,它应该只检查>=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.例如,如果有: 2023-01-01, 2023-01-02, 2024-01-01, 2019-01-01 ,它不应该给我 3 作为答案,但应该给我 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.因为,它应该计算每个日期之间有多少天,如果一个或另一个之间的天数小于 90 天,它应该将它们“合并”为一个。

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.再举一个例子,如果我有一组: '2023-01-01, 2023-10-01, 2024-10-01' 我应该得到 3 结果。

... 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.每行确实有 12 个日期。 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..但是它们应该按 2 分组。所以第一个和第二个,第三个和第四个,第五个和第六个等等。

So let's take my first example :所以让我们以我的第一个例子为例:

2023-01-01, 2023-01-02, 2024-01-01, 2019-01-01 . 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.它应该计算这些组中每一组之间的差异,因此 2023-01-01 与 2023-01-02,AND,2024-01-01 与 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.任何时候有 2 个相隔 <90 天的日期,都应将它们计为 1。如果它们相隔 >90 天,则应将它们计为 2。因此,如果我计算 2023-01-01 和 2023-01 之间的差异-02,它给了我 <90 天。 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.所以它是 1。然后我用 2019-01-01 计算 2024-01-01,但由于 2019-01-01 不符合第一个条件,它应该被忽略并将 2024-01-01 算作一个。 Finaly it should add the first answer to the second, and give me the final answer, which is 2.最后它应该将第一个答案添加到第二个答案,并给我最终答案,即 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.如果不是 2019-01-01,我有 2024-05-01,我应该给我 2 作为该组的答案,因为 2024-01-01 - 2024-05-01 = > 90 天。 Then finaly I would add 1 + 2 and the final answer would be 3.然后最后我会加上 1 + 2,最终答案是 3。

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 datetime

line = "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 += 2
    else:
        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 datetime

def getTotal(line):
    dates = list(datetime.strptime(elem, '%Y-%m-%d') for elem in line.split(', '))
    total = 0
    for i in range(0, len(dates), 2):
        delta = dates[i+1] - dates[i]
        if delta.days > 90:
            total += 2
        else:
            total += 1
    return total

line = "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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM