Doing something wrong with datetime and .append

“”"
I’m probably doing some wrong here but can’t see what…Basically I want to extract a datetime from a Tuple containing Lists of info. So far so good.
new_list 0 (None, ‘2022-11-14 07:18:06’, ‘2022-11-14 21:27:19.824344’, ‘+64274946632’, ‘Null’, ‘Null’, ‘BLM 323’, 10, ‘BLM 323 2197.3 Prestart Ok’, 2197.3, 0.0, None, 1)

So I extract the datetime and convert it to a date which is then .appended into a List.
line_datetime = datetime.datetime.strptime(new_log_list[i][db_Hours.Ptr_Hours.DATETIME_FROM_MESSAGE_PTR.value], ‘%Y-%m-%d %H:%M:%S’)
print(‘175 line_date’,line_datetime)

Result : 175 line_date 2022-11-14 07:18:06 (so far so good)

My problem is, instead of just appending the date, it’s appending text as well.
weekly_date_list.append(line_datetime.date())
print(‘181 weekly_date_list -’, weekly_date_list)

    Result : 181 weekly_date_list - [datetime.date(2022, 11, 14)]

It seems to be converting it back to text and can’t see why? Any thoughts?

“”"

Please post a short piece of code that shows the problem.
I cannot tell from you prose what code you are running.

maybe,

weekly_date_list.append(str(line_datetime.date()))
if len(new_log_list):
    # start_dt = datetime.date(2022, 5, 16)
    # stop_dt = datetime.date(2022, 5, 22)
    # Generally we want a full week for machine report
    # So need to figure out what last Sunday's date was and use that to as finishing date.

    # create info for spreadsheet'

    new_list = []
    weekly_date_list = []
    weekly_weather_list = []
    weekly_operator_list = []
    weekly_plant_num_list = []

    for i in range(len(new_log_list)):
        line_datetime = datetime.datetime.strptime(new_log_list[i][db_Hours.Ptr_Hours.DATETIME_FROM_MESSAGE_PTR.value], '%Y-%m-%d %H:%M:%S')
        # line_date = line_date.date(new_log_list[i][4])
        #print('line_date =',line_datetime)

        if (line_datetime.date() >= start_dt) and (line_datetime.date() <= week_ending) :
            # As search continues we end up with a list of tuples (this is demo data set up in Hours DB)
            # 146 new_list - Message Date       Added Date                Phone number User index, Name, PlantID, Plant Idx, Log info      , Start Hrs, Stop Hr, Company Idx, Site Idx
            # [(None, '2022-09-22 07:27:31', '2022-07-18 22:49:58.617307', '+64274946632', '1', 'Brent', 'BSLE01', '4', 'bsle01 prestart ok', 10030.0, 0.0, 1, 1),
            # (None, '2022-09-23 07:18:21', '2022-07-19 21:56:19.395687', '+64274946632', '1', 'Brent', 'BSLE01', '4', 'ok', 10039.0, 0.0, 1, 1),
            # (None, '2022-09-23 17:33:26', '2022-07-19 21:56:19.442523', '+64274946632', '1', 'Brent', 'BSLE01', '4', 'finished', 0.0, 9969.0, 1, 1),
            # (None, '2022-09-24 07:12:45', '2022-08-29 07:13:50.451340', '+64274946632', '1', 'Brent', 'BSLE01', 4, ' Prestart ok', 10045.0, 0.0, 1, 1),
            # (None, '2022-09-25 07:20:45', '2022-08-29 07:13:50.451340', '+64274946632', '1', 'Brent', 'BSLE01', 4, 'Prestart ok', 10058.0, 0.0, 1, 1),
            # (None, '2022-09-26 07:15:45', '2022-08-29 07:13:50.451340', '+64274946632', '1', 'Brent', 'BSLE01', 4, 'Prestart ok', 10063.0, 0.0, 1, 1),
            # (None, '2022-09-27 07:18:45', '2022-08-29 07:13:50.451340', '+64274946632', '1', 'Brent', 'BSLE01', 4, 'OK', 10071.0, 0.0, 1, 1)]
            new_list.append(new_log_list[i])
            print('177 new_log_list line:',i,new_log_list[i])

            for j in range(len(new_list)):
               print('180 new_list',j, new_list[j])

            print('182 line_date',line_datetime)

            striped_date = line_datetime.date()
            print('185 striped_date',striped_date)

            weekly_date_list.append(striped_date)
            print('188 weekly_date_list -', weekly_date_list)

            # As we loop through append the relevant info onto each item group.
            # weekly_date_list.append(new_log_list[i][1]) # will create a list of dates
            # weekly_weather_list.append(new_log_list[i]) not available from hours DB
            weekly_operator_list.append(new_log_list[i][5])
            weekly_plant_num_list.append(new_log_list[i][6])

            # next pull out the hours

    print('198 molly_reports.process_weekly_report weekly_date_list =',weekly_date_list)
    print('199 molly_reports.process_weekly_report weekly_operator_list =', weekly_operator_list)
    print('200 molly_reports.process_weekly_report weekly_plant_num_list =', weekly_plant_num_list)

This is the result in the run log:
177 new_log_list line: 76 (None, ‘2022-11-14 07:18:06’, ‘2022-11-14 21:27:19.824344’, ‘+64274946632’, ‘Null’, ‘Null’, ‘BLM 323’, 10, ‘BLM 323 2197.3 Prestart Ok’, 2197.3, 0.0, None, 1)
180 new_list 0 (None, ‘2022-11-14 07:18:06’, ‘2022-11-14 21:27:19.824344’, ‘+64274946632’, ‘Null’, ‘Null’, ‘BLM 323’, 10, ‘BLM 323 2197.3 Prestart Ok’, 2197.3, 0.0, None, 1)
182 line_date 2022-11-14 07:18:06
185 striped_date 2022-11-14
188 weekly_date_list - [datetime.date(2022, 11, 14)]
line_date = 2022-11-15 07:15:34
177 new_log_list line: 77 (None, ‘2022-11-15 07:15:34’, ‘2022-11-16 04:37:02.965855’, ‘+64274946632’, ‘Null’, ‘Null’, ‘BLM 323’, 10, ‘BLM 323 2206.3 Prestart Ok’, 2206.3, 0.0, None, 1)
180 new_list 0 (None, ‘2022-11-14 07:18:06’, ‘2022-11-14 21:27:19.824344’, ‘+64274946632’, ‘Null’, ‘Null’, ‘BLM 323’, 10, ‘BLM 323 2197.3 Prestart Ok’, 2197.3, 0.0, None, 1)
180 new_list 1 (None, ‘2022-11-15 07:15:34’, ‘2022-11-16 04:37:02.965855’, ‘+64274946632’, ‘Null’, ‘Null’, ‘BLM 323’, 10, ‘BLM 323 2206.3 Prestart Ok’, 2206.3, 0.0, None, 1)
182 line_date 2022-11-15 07:15:34
185 striped_date 2022-11-15
188 weekly_date_list - [datetime.date(2022, 11, 14), datetime.date(2022, 11, 15)]

I keep ending up with [datetime.date(2022, 11, 14), instead of [2022-11-14,2022-11-15)]
It’s adding datetime.date() and date is in different format

It’ll be something stupid that I don’t understand yet…

I’m probably doing some wrong here but can’t see what…Basically I
want to extract a datetime from a Tuple containing Lists of info. So
far so good.

It would help if you surrounded your code snippets with code fences
(triple backticks), eg:

 ```
 paste code here
 ```

Anyway, I think you mean that your variable new_log_list is a list
containing this tuple:

 (None, '2022-11-14 07:18:06', '2022-11-14 21:27:19.824344', '+64274946632', 'Null', 'Null', 'BLM 323', 10, 'BLM 323 2197.3 Prestart Ok', 2197.3, 0.0, None, 1)

So I extract the datetime and convert it to a date which is then .appended into a List.
line_datetime = datetime.datetime.strptime(new_log_list[i][db_Hours.Ptr_Hours.DATETIME_FROM_MESSAGE_PTR.value], ‘%Y-%m-%d %H:%M:%S’)

It would do you some good to break this expression up into pieces and
examine each piece. Eg:

 entry = new_log_list[i]
 timestamp_txt = entry[db_Hours.Ptr_Hours.DATETIME_FROM_MESSAGE_PTR.value]
 line_datetime =_ datetime.datetime.strptime(timestamp_txt, '%Y-%m-%d H:%M:%S')

and examine each variable in turn.

However, it appears that it was working, based on your output:

 print('175 line_date',line_datetime)
 175 line_date 2022-11-14 07:18:06

My problem is, instead of just appending the date, it’s appending text
as well.
weekly_date_list.append(line_datetime.date())

Let’s look at this. line_datetime is a datetime.datetime object.
line_datetime.date() is a datetime.date object. (It is unfortunate
that the module name and the datetime type name are the same, but
there we are.)

 print('181 weekly_date_list -', weekly_date_list)
 181 weekly_date_list - [datetime.date(2022, 11, 14)]

It seems to be converting it back to text and can’t see why? Any
thoughts?

I think you’re misinterpreting the output.

The print() function inherently prints text, that’s what it is for.
It does this by calling str() on each of its arguments, then
outputting the result. You’ve got 2 arguments:

  • '181 weekly_date_list -', which is a str
  • weekly_date_list: which is a list

As you might imagine, str('181 weekly_date_list -') is the same
string. str of a list poduces a Python transcription of the list,
calling repr() on each member. repr of a datetime.date looks like
this:

 datetime.date(2022, 11, 14)

So I would say that in fact you have got correct stuff in your list.

You can check more precisely:

 print('weekly_date_list -', type(weekly_date_list), len(weekly_date_list))

which should tell you that you have a list of length 1.

 d = weekly_date_list[0]
 print('date -', type(d), repr(d), str(d))

which should tell yopu that you have a datetime.date object and show
you the repr() form and the str() form.

I think you’ll find it is all good.

Cheers,
Cameron Simpson cs@cskk.id.au

Thanks Cameron! I’ll digest your comments over a coffee. I have lots to learn!. This project started as simple text based project to track the machine hours on my digger for invoicing etc… It’s got complicated trying to deal with simple Natural Language from texts (well if you can call Texts NL). e.g. Molly has to turn something like ‘BSL E01 2345.6 Prestart ok’ split it into data for SQLite3. Then on request, (Get Weekly Report BSLE01) grab the data and create a PDF spreadsheet and email it to the requester.

Open ended indeed! Sounds like a lot of fun. Cheers, Cameron

[quote=“Cameron Simpson, post:5, topic:21432, username:cameron”]

print('weekly_date_list -', type(weekly_date_list), len(weekly_date_list))

which should tell you that you have a list of length 1.

As I said, LOTS to learn,
185 striped_date 2022-11-14
188 weekly_date_list - [datetime.date(2022, 11, 14)]
weekly_date_list - <class 'list'> 1
date - <class 'datetime.date'> datetime.date(2022, 11, 14) 2022-11-14

And voila! there it is in str(d). Just got to figure out how to pull the str version out, but I can read up on that.

Thanks!

Thank you that did the job, Cameron reminded me about the various parts involved
d = weekly_date_list[0]
print(‘date -’, type(d), repr(d), str(d))

All made sense!

Personally I wouldn’t convert it to a str until I needed a str. For example, to put it into some report. It is usually better to keep your data in its parsed form (i.e. as a datetime or a date), and to convert to str (or whatever) when you need to print it for a human to read, for example onto an invoice.

So I’d be appending the date to the list, as you’re already doing. And just calling str(the_date_here) as needed, if needed. If you’ve got a date or datetime you get free access to its strftime() method which lets you customise how the date gets printed, for example. And you can do arithmetic, such as subtracting 2 datetimes to get a timedelta, whcih will tell you for example how many hours are between the two datetimes. Etc etc.

Finally, don’t forget that print() calls str() at print time. So this:

print(your_date_value_here)

prints out what you sem to be after above.

Cheers,
Cameron

Thank you again Cameron!
This part of the code is processing info Queried from SQLite3. It pulls out all the relevant info which is then stored in a dictionary with all keys as the spreadsheet cell numbers. After that another module uses the dictionary info to update a spreadsheet.

I’m self taught and really have no idea if this is the right way doing something like this but it works well so far.

Small part of code below

“”"

Set up cell pointers

cell_heading1 = ‘A2’
cell_site_name = ‘D2’
cell_site_designation = ‘G2’
cell_site_stage_num = ‘H2’

initialise dictionary

cell_data = {
cell_heading1: ‘’, # ‘A2’,
cell_site_name: ‘’, # D2’
cell_site_designation: ‘’, # G2’
cell_site_stage_num: ‘’, # H2’
cell_weekend_date: ‘’, # H5’
.
}
Dictionary is updated from another module then spreadsheet is created and loaded.

This function takes the info from the Spreadsheet Dictionary and Updates the Spreadsheet

def fill_in_the_spreadsheet():
print(“147 fill_in_the_spreadsheet”)
print(“148 cell_heading1”,cell_data.get(cell_heading1,‘Null’))
wks.update(cell_heading1,cell_data.get(cell_heading1,‘Null’)) # 221122
wks.update(cell_site_name, cell_data.get(cell_site_name,‘Null’)) # 221122
wks.update(cell_site_designation, cell_data.get(cell_site_designation,‘Null’)) # 221122

After that the Google Sheet is shared with the requester.

Again I have no idea if this the right way of doing this but it works. Google sheets s a bit slow so will eventually have to find another solution.

“”"
:grinning:

Thank you again Cameron!
This part of the code is processing info Queried from SQLite3. It pulls out all the relevant info which is then stored in a dictionary with all keys as the spreadsheet cell numbers. After that another module uses the dictionary info to update a spreadsheet.

I’m self taught and really have no idea if this is the right way doing something like this but it works well so far.

“Works well” is the primary criterion.

“”"

This function takes the info from the Spreadsheet Dictionary and Updates the Spreadsheet

def fill_in_the_spreadsheet():
print(“147 fill_in_the_spreadsheet”)
print(“148 cell_heading1”,cell_data.get(cell_heading1,‘Null’))
wks.update(cell_heading1,cell_data.get(cell_heading1,‘Null’)) # 221122
wks.update(cell_site_name, cell_data.get(cell_site_name,‘Null’)) # 221122
wks.update(cell_site_designation, cell_data.get(cell_site_designation,‘Null’)) # 221122
“”"
After that the Google Sheet is shared with the requester.

Again I have no idea if this the right way of doing this but it works.
Google sheets s a bit slow so will eventually have to find another
solution.

Google sheets is very slow.

You might do better to export a CSV or Excel spreadsheet and share that
via email or dropbox or google sheets. If the requester only needs to
see the data and not change them.

Cheers,
Cameron Simpson cs@cskk.id.au