Get the Month with year including one character in python - python

I'm trying to get the months list with year like [
"2019M10",
"2019M11",
"2019M12",
"2020M01",
"2020M02",
"2020M03",
"2020M04",
"2020M05",
"2020M06",
"2020M07",
"2020M08",
"2020M09",
"2020M10",
"2020M11",
"2020M12",
"2021M01",
"2021M02",
"2021M03",
"2021M04",
"2021M05"
]
Following Code Sample I'm using
import datetime
import json
from_year = 2018
last_year = datetime.datetime.now().year
print(last_year)
year_list = list(range(from_year, last_year))
new_month = []
for all_year in year_list:
all_months = [str(all_year)+'M'+str(i) for i in list(range(1,13))]
all_months.extend(all_months)
print(all_months )
months = json.dumps(all_months)
print(months)
I'm not getting the desired output.

Use Timestamp.to_period for actual year and month, create PeriodIndex by period_range and then convert values to format YYYYMmm by PeriodIndex.strftime:
from_year = 2018
last_year = pd.to_datetime('now').to_period('m')
print(last_year)
2021-07
months = pd.period_range(from_year, last_year, freq='M').strftime('%YM%m').tolist()
print (months)
['2018M01', '2018M02', '2018M03', '2018M04', '2018M05', '2018M06', '2018M07', '2018M08',
'2018M09', '2018M10', '2018M11', '2018M12', '2019M01', '2019M02', '2019M03', '2019M04',
'2019M05', '2019M06', '2019M07', '2019M08', '2019M09', '2019M10', '2019M11', '2019M12',
'2020M01', '2020M02', '2020M03', '2020M04', '2020M05', '2020M06', '2020M07', '2020M08',
'2020M09', '2020M10', '2020M11', '2020M12', '2021M01', '2021M02', '2021M03', '2021M04',
'2021M05', '2021M06', '2021M07']
If need all months add next year and then slice last value of months:
from_year = 2018
last_year = pd.to_datetime('now').year + 1
print(last_year)
2022
months = pd.period_range(from_year, last_year, freq='M')[:-1].strftime('%YM%m').tolist()
print (months)
['2018M01', '2018M02', '2018M03', '2018M04', '2018M05', '2018M06', '2018M07', '2018M08',
'2018M09', '2018M10', '2018M11', '2018M12', '2019M01', '2019M02', '2019M03', '2019M04',
'2019M05', '2019M06', '2019M07', '2019M08', '2019M09', '2019M10', '2019M11', '2019M12',
'2020M01', '2020M02', '2020M03', '2020M04', '2020M05', '2020M06', '2020M07', '2020M08',
'2020M09', '2020M10', '2020M11', '2020M12', '2021M01', '2021M02', '2021M03', '2021M04',
'2021M05', '2021M06', '2021M07', '2021M08', '2021M09', '2021M10', '2021M11', '2021M12']
Your solution with nested list comprehension with flatten:
from_year = 2018
last_year = datetime.datetime.now().year
print(last_year)
2021
year_list = list(range(from_year, last_year))
months = [f'{all_year}M{i:02}' for all_year in year_list for i in list(range(1,13))]
print (months)
['2018M01', '2018M02', '2018M03', '2018M04', '2018M05', '2018M06', '2018M07', '2018M08',
'2018M09', '2018M10', '2018M11', '2018M12', '2019M01', '2019M02', '2019M03', '2019M04',
'2019M05', '2019M06', '2019M07', '2019M08', '2019M09', '2019M10', '2019M11', '2019M12',
'2020M01', '2020M02', '2020M03', '2020M04', '2020M05', '2020M06', '2020M07', '2020M08',
'2020M09', '2020M10', '2020M11', '2020M12', '2021M01', '2021M02', '2021M03', '2021M04',
'2021M05', '2021M06', '2021M07', '2021M08', '2021M09', '2021M10', '2021M11', '2021M12']

you are creating a new list every time you loop and extending it .So the last data is getting wiped off and filled with the latest data and you are extending it .So the data is appearing twice.
The solution given by #jezarel is most efficient, but then you can make these modification
import datetime
import json
from_year = 2018
last_year = datetime.datetime.now().year
print(last_year)
year_list = list(range(from_year, last_year))
print(year_list)
new_month = []
all_months=[]
for all_year in year_list:
new_all_months = [str(all_year)+'M'+str(i) for i in list(range(1,13))]
all_months.extend(new_all_months)

Related

Whats wrong with this code for checking age?

I want to know if inputed date of birth is over 18 or under.
def is_under_18(birth):
now = date.today()
return (
now.year - birth.year < 18
or now.year - birth.year == 18 and (
now.month < birth.month
or now.month == birth.month and now.day <= birth.day
)
)
And then:
year = int(input("Year born: "))
month = int(input("Month born: "))
day = int(input("Day born: "))`
birth = date(year,month,day)
if is_under_18(birth):
print('Under 18')
else:
print('Adult')
However, the only thing is, say I add a user which his birthday is the 25th of November 2004. The program lets me add it because it does not count the month. If I add a user which was born the 1st of January 2005, it doesn't allow me because 2022-2005=17.
Your original code doesn't seem to have a problem with the dates you mention, but does have a bug as Nov 22, 2004 is "Under 18" and today's date is Nov 22, 2022 (18th birthday). Use now.day < birth.day instead.
But if you compute the birthday required to be 18 by replacing today's year with 18 less, then directly compare the dates, you don't have to have a complicated comparison:
from datetime import date
def is_under_18(birth):
# today = date.today()
today = date(2022,11,22) # for repeatability of results
born_on_or_before = today.replace(year=today.year - 18)
return birth > born_on_or_before
print(f'Today is {date.today()}')
for year,month,day in [(2004,11,21), (2004,11,22), (2004,11,23), (2004,11,25), (2005,1,1)]:
birth = date(year,month,day)
if is_under_18(birth):
print(f'{birth} Under 18')
else:
print(f'{birth} Adult')
Output:
Today is 2022-11-22
2004-11-21 Adult
2004-11-22 Adult
2004-11-23 Under 18
2004-11-25 Under 18
2005-01-01 Under 18

Not able to figure how to write if-else statement for one of the user defined cases in python

I have two API.
Australia API- This API works only for year 1985 to 2024.
USA API- I wanted this API need should work only before 1985.
Taking 4 things from user.
-Start Year
-End Year
-latitude
-longitude
sample command: python test.py -latitude '' -longitude '' -startYear '' -endYear ''
User can enter 3 ways of input.
Case 1. Start year=before 1985, end year= After 1985 ---->both AUSTRALIA and USA api run.
Case 2. Start year=At 1985 or later, end year= after 1985 ---->only AUSTRALIA api should run.
Case 3. Start year=before 1985, end year=before 1985 ------>only USA api run
Problem is that I am not able to figure out how to write the code for Case 1 after writing the code for case 2(Australia API) and case 3(USA API).
import requests
import json
import argparse
import time
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser(description="Process some integers.")
parser.add_argument("-latitude", help="Latitude(Degress)")
parser.add_argument("-longitude", help="Longitude(Degress)")
parser.add_argument("-startYear", help="Start of the Year")
parser.add_argument("-endYear", help="End of the Year")
parser.add_argument("--verbose", help="display processing information")
start = time.time()
def main(latitude,longitude,startYear,endYear,verbose):
parameters = {
"latd": latitude, # [deg]
"latm": 00, # [deg]
"lats": 00, # [deg]
"lond": longitude, # [deg]
"lonm": 00, # [deg]
"lons": 00, # [deg]
"elev" : 00, # [km]
"year" : None, # [YYYY]
"month" : '07', # [MM]
"day": '01', # [DD]
"Ein": 'D' # [Model]
}
hostname = "https://api.geomagnetism.ga.gov.au/agrf"
hostname1 = "http://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination?%s"
df_1=pd.DataFrame()
for year in range(startYear, endYear):
if (startYear>=1985 and endYear>1985):
-----
elif (startYear<1985 and endYear<1985):
-------
if endYear < 1985:
if startYear < 1985:
# Case 3
elif startYear >= 1985:
# Case 2
elif startYear < 1985:
# Case 1
else:
# Case where endYear < 1985 and startYear > 1985 (probably an input error)
if end > 1985:
australia
if start < 1985:
usa
else:
usa

How does Python convert date value from excel

I am reading a csv file with a CDATE column. The structure of the column is:
|CDATE |
|08/28/2018|
|08/28/2018|
|08/29/2018|
|08/30/2018|
|09/02/2018|
|09/04/2018|
...
|04/10/2019|
As you can see there is duplicate date as well as missing dates in this column, and I would like to find the missing dates and add them to my dataframe.
My code is:
import matplotlib.pyplot as plt
warnings.filterwarnings("ignore")
plt.style.use('fivethirtyeight')
import pandas as pd
df = pd.read_csv("XXX.csv")
dateCol = df['CDATE'].values.tolist()
dates = pd.to_datetime(dateCol, format='%m/%d/%Y')
startDate = dates.min()
endDate = dates.max()
df = df.sort_values('CDATE')
df_plastic = df['PLASTIC'].unique()
dateRange = pd.date_range(startDate, endDate)
df_date = df['CDATE'].unique()
for cursorDate in dateRange:
if (cursorDate in df_date) is False:
print('Data is missing date {} from range {}'.format(cursorDate, df_date))
But the output is:
Data is missing date 2019-02-21 00:00:00 from ['01/01/2019' '01/02/2019' '01/03/2019' '01/04/2019' '01/05/2019'
'01/07/2019' '01/08/2019' '01/09/2019' '01/10/2019' '01/11/2019'
'01/12/2019' '01/14/2019' '01/15/2019' '01/16/2019' '01/17/2019'
'01/18/2019' '01/19/2019' '01/21/2019' '01/22/2019' '01/23/2019'
'01/24/2019' '01/25/2019' '01/26/2019' '01/28/2019' '01/29/2019'
'01/30/2019' '01/31/2019' '02/01/2019' '02/02/2019' '02/04/2019'
'02/05/2019' '02/06/2019' '02/07/2019' '02/08/2019' '02/09/2019'
'02/11/2019' '02/12/2019' '02/13/2019' '02/14/2019' '02/15/2019'
'02/16/2019' '02/19/2019' '02/20/2019' '02/21/2019' '02/22/2019'
'02/23/2019' '02/25/2019' '02/26/2019' '02/27/2019' '02/28/2019'
'03/01/2019' '03/02/2019' '03/03/2019' '03/04/2019' '03/05/2019'
'03/06/2019' '03/07/2019' '03/08/2019' '03/09/2019' '03/11/2019'
'03/12/2019' '03/13/2019' '03/14/2019' '03/15/2019' '03/16/2019'
'03/18/2019' '03/19/2019' '03/20/2019' '03/21/2019' '03/22/2019'
'03/23/2019' '03/25/2019' '03/26/2019' '03/27/2019' '03/28/2019'
'03/29/2019' '03/30/2019' '04/01/2019' '04/02/2019' '04/03/2019'
'04/04/2019' '04/05/2019' '04/06/2019' '04/08/2019' '04/09/2019'
'04/10/2019' '05/29/2018' '05/30/2018' '05/31/2018' '06/01/2018'
'06/02/2018' '06/04/2018' '06/05/2018' '06/06/2018' '06/07/2018'
'06/08/2018' '06/09/2018' '06/11/2018' '06/12/2018' '06/13/2018'
'06/14/2018' '06/15/2018' '06/16/2018' '06/18/2018' '06/19/2018'
'06/20/2018' '06/21/2018' '06/22/2018' '06/23/2018' '06/25/2018'
'06/26/2018' '06/27/2018' '06/28/2018' '06/29/2018' '06/30/2018'
'07/03/2018' '07/04/2018' '07/05/2018' '07/06/2018' '07/07/2018'
'07/09/2018' '07/10/2018' '07/11/2018' '07/12/2018' '07/13/2018'
'07/14/2018' '07/16/2018' '07/17/2018' '07/18/2018' '07/19/2018'
'07/20/2018' '07/21/2018' '07/23/2018' '07/24/2018' '07/25/2018'
'07/26/2018' '07/27/2018' '07/28/2018' '07/30/2018' '07/31/2018'
'08/01/2018' '08/02/2018' '08/03/2018' '08/04/2018' '08/07/2018'
'08/08/2018' '08/09/2018' '08/10/2018' '08/11/2018' '08/13/2018'
'08/14/2018' '08/15/2018' '08/16/2018' '08/17/2018' '08/18/2018'
'08/20/2018' '08/21/2018' '08/22/2018' '08/23/2018' '08/24/2018'
'08/25/2018' '08/27/2018' '08/28/2018' '08/29/2018' '08/30/2018'
'08/31/2018' '09/01/2018' '09/04/2018' '09/05/2018' '09/06/2018'
'09/07/2018' '09/08/2018' '09/10/2018' '09/11/2018' '09/12/2018'
'09/13/2018' '09/14/2018' '09/15/2018' '09/17/2018' '09/18/2018'
'09/19/2018' '09/20/2018' '09/21/2018' '09/22/2018' '09/24/2018'
'09/25/2018' '09/26/2018' '09/27/2018' '09/28/2018' '09/29/2018'
'10/01/2018' '10/02/2018' '10/03/2018' '10/04/2018' '10/05/2018'
'10/06/2018' '10/09/2018' '10/10/2018' '10/11/2018' '10/12/2018'
'10/13/2018' '10/15/2018' '10/16/2018' '10/17/2018' '10/18/2018'
'10/19/2018' '10/20/2018' '10/22/2018' '10/23/2018' '10/24/2018'
'10/25/2018' '10/26/2018' '10/29/2018' '10/30/2018' '10/31/2018'
'11/01/2018' '11/02/2018' '11/03/2018' '11/05/2018' '11/06/2018'
'11/07/2018' '11/08/2018' '11/09/2018' '11/10/2018' '11/13/2018'
'11/14/2018' '11/15/2018' '11/16/2018' '11/18/2018' '11/19/2018'
'11/20/2018' '11/21/2018' '11/22/2018' '11/23/2018' '11/24/2018'
'11/26/2018' '11/27/2018' '11/28/2018' '11/29/2018' '11/30/2018'
'12/01/2018' '12/03/2018' '12/04/2018' '12/05/2018' '12/06/2018'
'12/07/2018' '12/08/2018' '12/09/2018' '12/10/2018' '12/11/2018'
'12/12/2018' '12/13/2018' '12/14/2018' '12/15/2018' '12/17/2018'
'12/18/2018' '12/19/2018' '12/20/2018' '12/21/2018' '12/22/2018'
'12/24/2018' '12/25/2018' '12/27/2018' '12/28/2018' '12/29/2018'
'12/31/2018']
Somehow the data type of cursorDate is changed to Timestamp, making the value comparison not work.
How is it converting the datetime formats?
Building on my comment above. Change the last line before your loop to this:
df_date = df['CDATE'].apply(pd.to_datetime).unique()

Python List of dates between x and z (dd//mm/year) [duplicate]

This question already has answers here:
How to print a date in a regular format?
(25 answers)
Closed 6 years ago.
from datetime import date, timedelta as td
d1 = date(1980, 1, 1)
d2 = date(2000, 1, 1)
delta = d2 - d1
for i in range(delta.days + 1):
print d1 + td(days=i)
How would I make it so it displays it in this form: dd/mm/yy
Thanks :)
Use strftime:
print your_date.strftime("%d/%m/%y")
Simply add this to your Print statement:
from datetime import date, timedelta as td
d1 = date(1980, 1, 1)
d2 = date(2000, 1, 1)
delta = d2 - d1
for i in range(delta.days + 1):
print ((d1 + td(days=i)).strftime("%d/%m/%Y"))
Output:
...
10/05/1996
11/05/1996
12/05/1996
13/05/1996
14/05/1996
15/05/1996
16/05/1996
17/05/1996
18/05/1996
19/05/1996
20/05/1996
21/05/1996
22/05/1996
23/05/1996
24/05/1996
25/05/1996
26/05/1996
27/05/1996
28/05/1996
29/05/1996
30/05/1996
31/05/1996
01/06/1996
02/06/1996
03/06/1996
04/06/1996
05/06/1996
06/06/1996
07/06/1996
08/06/1996
09/06/1996
10/06/1996
11/06/1996
12/06/1996
13/06/1996
14/06/1996
15/06/1996
16/06/1996
17/06/1996
18/06/1996
19/06/1996
20/06/1996
21/06/1996
22/06/1996
23/06/1996
24/06/1996
25/06/1996
26/06/1996
27/06/1996
28/06/1996
29/06/1996
30/06/1996
01/07/1996
02/07/1996
03/07/1996
04/07/1996
05/07/1996
06/07/1996
07/07/1996
08/07/1996
09/07/1996
10/07/1996
11/07/1996
12/07/1996
13/07/1996
14/07/1996
15/07/1996
16/07/1996
17/07/1996
18/07/1996
19/07/1996
20/07/1996
21/07/1996
22/07/1996
23/07/1996
24/07/1996
25/07/1996
26/07/1996
27/07/1996
28/07/1996
29/07/1996
30/07/1996
31/07/1996
01/08/1996
02/08/1996
03/08/1996
04/08/1996
05/08/1996
06/08/1996
07/08/1996
08/08/1996
09/08/1996
10/08/1996
11/08/1996
12/08/1996
13/08/1996
14/08/1996
15/08/1996
16/08/1996
17/08/1996
18/08/1996
19/08/1996
20/08/1996
21/08/1996
22/08/1996
23/08/1996
24/08/1996
25/08/1996
26/08/1996
27/08/1996
28/08/1996
29/08/1996
30/08/1996
31/08/1996
01/09/1996
02/09/1996
03/09/1996
04/09/1996
05/09/1996
06/09/1996
07/09/1996
08/09/1996
09/09/1996
10/09/1996
11/09/1996
12/09/1996
13/09/1996
14/09/1996
15/09/1996
16/09/1996
17/09/1996
18/09/1996
19/09/1996
20/09/1996
21/09/1996
22/09/1996
23/09/1996
24/09/1996
25/09/1996
26/09/1996
27/09/1996
28/09/1996
29/09/1996
30/09/1996
01/10/1996
02/10/1996
03/10/1996
04/10/1996
05/10/1996
06/10/1996
07/10/1996
08/10/1996
09/10/1996
10/10/1996
11/10/1996
12/10/1996
13/10/1996
14/10/1996
15/10/1996
16/10/1996
17/10/1996
18/10/1996
19/10/1996
20/10/1996
21/10/1996
22/10/1996
23/10/1996
24/10/1996
25/10/1996
26/10/1996
27/10/1996
28/10/1996
29/10/1996
30/10/1996
31/10/1996
01/11/1996
02/11/1996
03/11/1996
04/11/1996
05/11/1996
06/11/1996
07/11/1996
08/11/1996
09/11/1996
10/11/1996
11/11/1996
12/11/1996
13/11/1996
14/11/1996
15/11/1996
16/11/1996
17/11/1996
18/11/1996
19/11/1996
20/11/1996
21/11/1996
22/11/1996
23/11/1996
24/11/1996
25/11/1996
26/11/1996
27/11/1996
28/11/1996
29/11/1996
30/11/1996
01/12/1996
02/12/1996
03/12/1996
04/12/1996
05/12/1996
06/12/1996
07/12/1996
08/12/1996
09/12/1996
10/12/1996
11/12/1996
12/12/1996
13/12/1996
14/12/1996
15/12/1996
16/12/1996
17/12/1996
18/12/1996
19/12/1996
20/12/1996
21/12/1996
22/12/1996
23/12/1996
24/12/1996
25/12/1996
26/12/1996
27/12/1996
28/12/1996
29/12/1996
30/12/1996
31/12/1996
01/01/1997
02/01/1997
03/01/1997
04/01/1997
05/01/1997
06/01/1997
07/01/1997
08/01/1997
09/01/1997
10/01/1997
11/01/1997
12/01/1997
13/01/1997
14/01/1997
15/01/1997
16/01/1997
17/01/1997
18/01/1997
19/01/1997
20/01/1997
21/01/1997
22/01/1997
23/01/1997
24/01/1997
25/01/1997
26/01/1997
27/01/1997
28/01/1997
29/01/1997
30/01/1997
31/01/1997
01/02/1997
02/02/1997
03/02/1997
04/02/1997
05/02/1997
06/02/1997
07/02/1997
08/02/1997
09/02/1997
10/02/1997
11/02/1997
12/02/1997
13/02/1997
14/02/1997
15/02/1997
16/02/1997
17/02/1997
18/02/1997
19/02/1997
20/02/1997
21/02/1997
22/02/1997
23/02/1997
24/02/1997
25/02/1997
26/02/1997
27/02/1997
28/02/1997
01/03/1997
02/03/1997
03/03/1997
04/03/1997
05/03/1997
06/03/1997
07/03/1997
08/03/1997
09/03/1997
10/03/1997
11/03/1997
12/03/1997
13/03/1997
14/03/1997
15/03/1997
16/03/1997
17/03/1997
18/03/1997
19/03/1997
20/03/1997
21/03/1997
22/03/1997
23/03/1997
24/03/1997
25/03/1997
26/03/1997
27/03/1997
28/03/1997
29/03/1997
30/03/1997
31/03/1997
01/04/1997
02/04/1997
03/04/1997
04/04/1997
05/04/1997
06/04/1997
07/04/1997
08/04/1997
09/04/1997
10/04/1997
11/04/1997
12/04/1997
13/04/1997
14/04/1997
15/04/1997
16/04/1997
17/04/1997
18/04/1997
19/04/1997
20/04/1997
21/04/1997
22/04/1997
23/04/1997
24/04/1997
25/04/1997
26/04/1997
27/04/1997
28/04/1997
29/04/1997
30/04/1997
01/05/1997
02/05/1997
03/05/1997
04/05/1997
05/05/1997
06/05/1997
07/05/1997
08/05/1997
09/05/1997
10/05/1997
11/05/1997
12/05/1997
13/05/1997
14/05/1997
15/05/1997
16/05/1997
17/05/1997
18/05/1997
19/05/1997
20/05/1997
21/05/1997
22/05/1997
23/05/1997
24/05/1997
25/05/1997
26/05/1997
27/05/1997
28/05/1997
29/05/1997
30/05/1997
31/05/1997
01/06/1997
02/06/1997
03/06/1997
04/06/1997
05/06/1997
06/06/1997
07/06/1997
08/06/1997
09/06/1997
10/06/1997
11/06/1997
12/06/1997
13/06/1997
14/06/1997
15/06/1997
16/06/1997
17/06/1997
18/06/1997
19/06/1997
20/06/1997
21/06/1997
22/06/1997
23/06/1997
24/06/1997
25/06/1997
26/06/1997
27/06/1997
28/06/1997
29/06/1997
30/06/1997
01/07/1997
02/07/1997
03/07/1997
04/07/1997
05/07/1997
06/07/1997
07/07/1997
08/07/1997
09/07/1997
10/07/1997
11/07/1997
12/07/1997
13/07/1997
14/07/1997
15/07/1997
16/07/1997
17/07/1997
18/07/1997
19/07/1997
20/07/1997
21/07/1997
22/07/1997
23/07/1997
24/07/1997
25/07/1997
26/07/1997
27/07/1997
28/07/1997
29/07/1997
30/07/1997
31/07/1997
01/08/1997
02/08/1997
03/08/1997
04/08/1997
05/08/1997
06/08/1997
07/08/1997
08/08/1997
09/08/1997
10/08/1997
11/08/1997
12/08/1997
13/08/1997
14/08/1997
15/08/1997
16/08/1997
17/08/1997
18/08/1997
19/08/1997
20/08/1997
21/08/1997
22/08/1997
23/08/1997
24/08/1997
25/08/1997
26/08/1997
27/08/1997
28/08/1997
29/08/1997
30/08/1997
31/08/1997
01/09/1997
02/09/1997
03/09/1997
04/09/1997
05/09/1997
06/09/1997
07/09/1997
08/09/1997
09/09/1997
10/09/1997
11/09/1997
12/09/1997
13/09/1997
14/09/1997
15/09/1997
16/09/1997
17/09/1997
18/09/1997
19/09/1997
20/09/1997
21/09/1997
22/09/1997
23/09/1997
24/09/1997
25/09/1997
26/09/1997
27/09/1997
28/09/1997
29/09/1997
30/09/1997
01/10/1997
02/10/1997
03/10/1997
04/10/1997
05/10/1997
06/10/1997
07/10/1997
08/10/1997
09/10/1997
10/10/1997
11/10/1997
12/10/1997
13/10/1997
14/10/1997
15/10/1997
16/10/1997
17/10/1997
18/10/1997
19/10/1997
20/10/1997
21/10/1997
22/10/1997
23/10/1997
24/10/1997
25/10/1997
26/10/1997
27/10/1997
28/10/1997
29/10/1997
30/10/1997
31/10/1997
01/11/1997
02/11/1997
03/11/1997
04/11/1997
05/11/1997
06/11/1997
07/11/1997
08/11/1997
09/11/1997
10/11/1997
11/11/1997
12/11/1997
13/11/1997
14/11/1997
15/11/1997
16/11/1997
17/11/1997
18/11/1997
19/11/1997
20/11/1997
21/11/1997
22/11/1997
23/11/1997
24/11/1997
25/11/1997
26/11/1997
27/11/1997
28/11/1997
29/11/1997
30/11/1997
01/12/1997
02/12/1997
03/12/1997
04/12/1997
05/12/1997
06/12/1997
07/12/1997
08/12/1997
09/12/1997
10/12/1997
11/12/1997
12/12/1997
13/12/1997
14/12/1997
15/12/1997
16/12/1997
17/12/1997
18/12/1997
19/12/1997
20/12/1997
21/12/1997
22/12/1997
23/12/1997
24/12/1997
25/12/1997
26/12/1997
27/12/1997
28/12/1997
29/12/1997
30/12/1997
31/12/1997
01/01/1998
02/01/1998
03/01/1998
04/01/1998
05/01/1998
06/01/1998
07/01/1998
08/01/1998
09/01/1998
10/01/1998
11/01/1998
12/01/1998
13/01/1998
14/01/1998
15/01/1998
16/01/1998
17/01/1998
18/01/1998
19/01/1998
20/01/1998
21/01/1998
22/01/1998
23/01/1998
24/01/1998
25/01/1998
26/01/1998
27/01/1998
28/01/1998
29/01/1998
30/01/1998
31/01/1998
01/02/1998
02/02/1998
03/02/1998
04/02/1998
05/02/1998
06/02/1998
07/02/1998
08/02/1998
09/02/1998
10/02/1998
11/02/1998
12/02/1998
13/02/1998
14/02/1998
15/02/1998
16/02/1998
17/02/1998
18/02/1998
19/02/1998
20/02/1998
21/02/1998
22/02/1998
23/02/1998
24/02/1998
25/02/1998
26/02/1998
27/02/1998
28/02/1998
01/03/1998
02/03/1998
03/03/1998
04/03/1998
05/03/1998
06/03/1998
07/03/1998
08/03/1998
09/03/1998
10/03/1998
11/03/1998
12/03/1998
13/03/1998
14/03/1998
15/03/1998
16/03/1998
17/03/1998
18/03/1998
19/03/1998
20/03/1998
21/03/1998
22/03/1998
23/03/1998
24/03/1998
25/03/1998
26/03/1998
27/03/1998
28/03/1998
29/03/1998
30/03/1998
31/03/1998
01/04/1998
02/04/1998
03/04/1998
04/04/1998
05/04/1998
06/04/1998
07/04/1998
08/04/1998
09/04/1998
10/04/1998
11/04/1998
12/04/1998
13/04/1998
14/04/1998
15/04/1998
16/04/1998
17/04/1998
18/04/1998
19/04/1998
20/04/1998
21/04/1998
22/04/1998
23/04/1998
24/04/1998
25/04/1998
26/04/1998
27/04/1998
28/04/1998
29/04/1998
30/04/1998
01/05/1998
02/05/1998
03/05/1998
04/05/1998
05/05/1998
06/05/1998
07/05/1998
08/05/1998
09/05/1998
10/05/1998
11/05/1998
12/05/1998
13/05/1998
14/05/1998
15/05/1998
16/05/1998
17/05/1998
18/05/1998
19/05/1998
20/05/1998
21/05/1998
22/05/1998
23/05/1998
24/05/1998
25/05/1998
26/05/1998
27/05/1998
28/05/1998
29/05/1998
30/05/1998
31/05/1998
01/06/1998
02/06/1998
03/06/1998
04/06/1998
05/06/1998
06/06/1998
07/06/1998
08/06/1998
09/06/1998
10/06/1998
11/06/1998
12/06/1998
13/06/1998
14/06/1998
15/06/1998
16/06/1998
17/06/1998
18/06/1998
19/06/1998
20/06/1998
21/06/1998
22/06/1998
23/06/1998
24/06/1998
25/06/1998
26/06/1998
27/06/1998
28/06/1998
29/06/1998
30/06/1998
01/07/1998
02/07/1998
03/07/1998
04/07/1998
05/07/1998
06/07/1998
07/07/1998
08/07/1998
09/07/1998
10/07/1998
11/07/1998
12/07/1998
13/07/1998
14/07/1998
15/07/1998
16/07/1998
17/07/1998
18/07/1998
19/07/1998
20/07/1998
21/07/1998
22/07/1998
23/07/1998
24/07/1998
25/07/1998
26/07/1998
27/07/1998
28/07/1998
29/07/1998
30/07/1998
31/07/1998
01/08/1998
02/08/1998
03/08/1998
04/08/1998
05/08/1998
06/08/1998
07/08/1998
08/08/1998
09/08/1998
10/08/1998
11/08/1998
12/08/1998
13/08/1998
14/08/1998
15/08/1998
16/08/1998
17/08/1998
18/08/1998
19/08/1998
20/08/1998
21/08/1998
22/08/1998
23/08/1998
24/08/1998
25/08/1998
26/08/1998
27/08/1998
28/08/1998
29/08/1998
30/08/1998
31/08/1998
01/09/1998
02/09/1998
03/09/1998
04/09/1998
05/09/1998
06/09/1998
07/09/1998
08/09/1998
09/09/1998
10/09/1998
11/09/1998
12/09/1998
13/09/1998
14/09/1998
15/09/1998
16/09/1998
17/09/1998
18/09/1998
19/09/1998
20/09/1998
21/09/1998
22/09/1998
23/09/1998
24/09/1998
25/09/1998
26/09/1998
27/09/1998
28/09/1998
29/09/1998
30/09/1998
01/10/1998
02/10/1998
03/10/1998
04/10/1998
05/10/1998
06/10/1998
07/10/1998
08/10/1998
09/10/1998
10/10/1998
11/10/1998
12/10/1998
13/10/1998
14/10/1998
15/10/1998
16/10/1998
17/10/1998
18/10/1998
19/10/1998
20/10/1998
21/10/1998
22/10/1998
23/10/1998
24/10/1998
25/10/1998
26/10/1998
27/10/1998
28/10/1998
29/10/1998
30/10/1998
31/10/1998
01/11/1998
02/11/1998
03/11/1998
04/11/1998
05/11/1998
06/11/1998
07/11/1998
08/11/1998
09/11/1998
10/11/1998
11/11/1998
12/11/1998
13/11/1998
14/11/1998
15/11/1998
16/11/1998
17/11/1998
18/11/1998
19/11/1998
20/11/1998
21/11/1998
22/11/1998
23/11/1998
24/11/1998
25/11/1998
26/11/1998
27/11/1998
28/11/1998
29/11/1998
30/11/1998
01/12/1998
02/12/1998
03/12/1998
04/12/1998
05/12/1998
06/12/1998
07/12/1998
08/12/1998
09/12/1998
10/12/1998
11/12/1998
12/12/1998
13/12/1998
14/12/1998
15/12/1998
16/12/1998
17/12/1998
18/12/1998
19/12/1998
20/12/1998
21/12/1998
22/12/1998
23/12/1998
24/12/1998
25/12/1998
26/12/1998
27/12/1998
28/12/1998
29/12/1998
30/12/1998
31/12/1998
01/01/1999
02/01/1999
03/01/1999
04/01/1999
05/01/1999
06/01/1999
07/01/1999
08/01/1999
09/01/1999
10/01/1999
11/01/1999
12/01/1999
13/01/1999
14/01/1999
15/01/1999
16/01/1999
17/01/1999
18/01/1999
19/01/1999
20/01/1999
21/01/1999
22/01/1999
23/01/1999
24/01/1999
25/01/1999
26/01/1999
27/01/1999
28/01/1999
29/01/1999
30/01/1999
31/01/1999
01/02/1999
02/02/1999
03/02/1999
04/02/1999
05/02/1999
06/02/1999
07/02/1999
08/02/1999
09/02/1999
10/02/1999
11/02/1999
12/02/1999
13/02/1999
14/02/1999
15/02/1999
16/02/1999
17/02/1999
18/02/1999
19/02/1999
20/02/1999
21/02/1999
22/02/1999
23/02/1999
24/02/1999
25/02/1999
26/02/1999
27/02/1999
28/02/1999
01/03/1999
02/03/1999
03/03/1999
04/03/1999
05/03/1999
06/03/1999
07/03/1999
08/03/1999
09/03/1999
10/03/1999
11/03/1999
12/03/1999
13/03/1999
14/03/1999
15/03/1999
16/03/1999
17/03/1999
18/03/1999
19/03/1999
20/03/1999
21/03/1999
22/03/1999
23/03/1999
24/03/1999
25/03/1999
26/03/1999
27/03/1999
28/03/1999
29/03/1999
30/03/1999
31/03/1999
01/04/1999
02/04/1999
03/04/1999
04/04/1999
05/04/1999
06/04/1999
07/04/1999
08/04/1999
09/04/1999
10/04/1999
11/04/1999
12/04/1999
13/04/1999
14/04/1999
15/04/1999
16/04/1999
17/04/1999
18/04/1999
19/04/1999
20/04/1999
21/04/1999
22/04/1999
23/04/1999
24/04/1999
25/04/1999
26/04/1999
27/04/1999
28/04/1999
29/04/1999
30/04/1999
01/05/1999
02/05/1999
03/05/1999
04/05/1999
05/05/1999
06/05/1999
07/05/1999
08/05/1999
09/05/1999
10/05/1999
11/05/1999
12/05/1999
13/05/1999
14/05/1999
15/05/1999
16/05/1999
17/05/1999
18/05/1999
19/05/1999
20/05/1999
21/05/1999
22/05/1999
23/05/1999
24/05/1999
25/05/1999
26/05/1999
27/05/1999
28/05/1999
29/05/1999
30/05/1999
31/05/1999
01/06/1999
02/06/1999
03/06/1999
04/06/1999
05/06/1999
06/06/1999
07/06/1999
08/06/1999
09/06/1999
10/06/1999
11/06/1999
12/06/1999
13/06/1999
14/06/1999
15/06/1999
16/06/1999
17/06/1999
18/06/1999
19/06/1999
20/06/1999
21/06/1999
22/06/1999
23/06/1999
24/06/1999
25/06/1999
26/06/1999
27/06/1999
28/06/1999
29/06/1999
30/06/1999
01/07/1999
02/07/1999
03/07/1999
04/07/1999
05/07/1999
06/07/1999
07/07/1999
08/07/1999
09/07/1999
10/07/1999
11/07/1999
12/07/1999
13/07/1999
14/07/1999
15/07/1999
16/07/1999
17/07/1999
18/07/1999
19/07/1999
20/07/1999
21/07/1999
22/07/1999
23/07/1999
24/07/1999
25/07/1999
26/07/1999
27/07/1999
28/07/1999
29/07/1999
30/07/1999
31/07/1999
01/08/1999
02/08/1999
03/08/1999
04/08/1999
05/08/1999
06/08/1999
07/08/1999
08/08/1999
09/08/1999
10/08/1999
11/08/1999
12/08/1999
13/08/1999
14/08/1999
15/08/1999
16/08/1999
17/08/1999
18/08/1999
19/08/1999
20/08/1999
21/08/1999
22/08/1999
23/08/1999
24/08/1999
25/08/1999
26/08/1999
27/08/1999
28/08/1999
29/08/1999
30/08/1999
31/08/1999
01/09/1999
02/09/1999
03/09/1999
04/09/1999
05/09/1999
06/09/1999
07/09/1999
08/09/1999
09/09/1999
10/09/1999
11/09/1999
12/09/1999
13/09/1999
14/09/1999
15/09/1999
16/09/1999
17/09/1999
18/09/1999
19/09/1999
20/09/1999
21/09/1999
22/09/1999
23/09/1999
24/09/1999
25/09/1999
26/09/1999
27/09/1999
28/09/1999
29/09/1999
30/09/1999
01/10/1999
02/10/1999
03/10/1999
04/10/1999
05/10/1999
06/10/1999
07/10/1999
08/10/1999
09/10/1999
10/10/1999
11/10/1999
12/10/1999
13/10/1999
14/10/1999
15/10/1999
16/10/1999
17/10/1999
18/10/1999
19/10/1999
20/10/1999
21/10/1999
22/10/1999
23/10/1999
24/10/1999
25/10/1999
26/10/1999
27/10/1999
28/10/1999
29/10/1999
30/10/1999
31/10/1999
01/11/1999
02/11/1999
03/11/1999
04/11/1999
05/11/1999
06/11/1999
07/11/1999
08/11/1999
09/11/1999
10/11/1999
11/11/1999
12/11/1999
13/11/1999
14/11/1999
15/11/1999
16/11/1999
17/11/1999
18/11/1999
19/11/1999
20/11/1999
21/11/1999
22/11/1999
23/11/1999
24/11/1999
25/11/1999
26/11/1999
27/11/1999
28/11/1999
29/11/1999
30/11/1999
01/12/1999
02/12/1999
03/12/1999
04/12/1999
05/12/1999
06/12/1999
07/12/1999
08/12/1999
09/12/1999
10/12/1999
11/12/1999
12/12/1999
13/12/1999
14/12/1999
15/12/1999
16/12/1999
17/12/1999
18/12/1999
19/12/1999
20/12/1999
21/12/1999
22/12/1999
23/12/1999
24/12/1999
25/12/1999
26/12/1999
27/12/1999
28/12/1999
29/12/1999
30/12/1999
31/12/1999
01/01/2000

How to calculate the total time a log file covers in Python 2.7?

So I have several log files, they are structured like this:
Sep 9 12:42:15 apollo sshd[25203]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=189.26.255.11
Sep 9 12:42:15 apollo sshd[25203]: pam_succeed_if(sshd:auth): error retrieving information about user ftpuser
Sep 9 12:42:17 apollo sshd[25203]: Failed password for invalid user ftpuser from 189.26.255.11 port 44061 ssh2
Sep 9 12:42:17 apollo sshd[25204]: Received disconnect from 189.26.255.11: 11: Bye Bye
Sep 9 19:12:46 apollo sshd[30349]: Did not receive identification string from 199.19.112.130
Sep 10 03:29:48 apollo unix_chkpwd[4549]: password check failed for user (root)
Sep 10 03:29:48 apollo sshd[4546]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=221.12.29.170 user=root
Sep 10 03:29:51 apollo sshd[4546]: Failed password for root from 221.12.29.170 port 56907 ssh2
There are more dates and times, But this is an example. I was wondering how I would calculate the total time that the file covers. I've tried a few things, and have had about 5 hours of no success.
I tried this first, and it was close, but it didn't work like I wanted it to, it kept repeating dates:
with open(filename, 'r') as file1:
lines = file1.readlines()
for line in lines:
linelist = line.split()
date2 = int(linelist[1])
time2 = linelist[2]
print linelist[0], linelist[1], linelist[2]
if date1 == 0:
date1 = date2
dates.append(linelist[0] + ' ' + str(linelist[1]))
if date1 < date2:
date1 = date2
ttimes.append(datetime.strptime(str(ltime1), FMT) - datetime.strptime(str(time1), FMT))
time1 = '23:59:59'
ltime1 = '00:00:00'
dates.append(linelist[0] + ' ' + str(linelist[1]))
if time2 < time1:
time1 = time2
if time2 > ltime1:
ltime1 = time2
If the entries are in a chronological order, you can just look at the first and at the last entry:
entries = lines.split("\n")
first_date = entries[0].split("apollo")[0]
last_date = entries[len(entries)-1].split("apollo")[0]
We don't have the year, so I took the current year. Read all the lines, convert the month to month index, and parse each date.
Then sort it (so works even if logs mixed) and take first & last item. Substract. Enjoy.
from datetime import datetime
months = ["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
current_year = datetime.now().year
dates = list()
with open(filename, 'r') as file1:
for line in file1:
linelist = line.split()
if linelist: # filter out possible empty lines
linelist[0] = str(months.index(linelist[0])) # convert 3-letter months to index
date2 = int(linelist[1])
z=datetime.strptime(" ".join(linelist[0:3])+" "+str(current_year),"%m %d %H:%M:%S %Y") # compose & parse the date
dates.append(z) # store in list
dates.sort() # sort the list
first_date = dates[0]
last_date = dates[-1]
# print report & compute time span
print("start {}, end {}, time span {}".format(first_date,last_date,last_date-first_date))
result:
start 2016-09-09 12:42:15, end 2016-09-10 03:29:51, time span 14:47:36
Note that it won't work properly between december 31st and january the 1st because of the missing year info. I suppose we could make a guess if we find January & December in the log then assume that it's january from the next year. Unsupported yet.

Categories