I have some code reading a list of filenames with dates included in them, and after splitting the strings to get the date, I want to create a list of datetime objects. However, with this code, the datetime list remains empty, while the list of date strings is appended correctly.
Code:
calib_files = self.optitrack_calibration_listbox.get(0, END)
format = '%Y-%m-%d %H.%M.%S'
calib_datetimes = []
calib_dates = []
for calib in calib_files:
calib_date = ' '.join(calib.split(' ')[1:3])
calib_dates.append(calib_date)
calib_datetimes.append(datetime.strptime(calib_date, format))
print(f'{calib_dates=}')
print(f'{calib_datetimes=}')
Output:
calib_files=('Cal 2021-10-11 11.35.52 (3DErr 4.949 mm) Poor.cal', 'Cal 2021-10-11 11.44.49 (3DErr 0.285 mm).cal', 'Cal 2021-10-28 09.01.21 (3DErr 0.656 mm) Excellent.cal', 'Cal 2021-11-04 09.54.00 (3DErr 0.336 mm).cal', 'Cal 2021-12-16 10.30.59 (3DErr 0.319 mm).cal', 'Cal 2021-12-16 13.47.08 (3DErr 0.350 mm).cal', 'Cal 2022-01-12 15.50.51 (3DErr 0.302 mm).cal', 'Cal
2022-01-17 16.43.21 (3DErr 0.239 mm).cal', 'Cal 2022-01-19 09.41.54 (3DErr 0.300 mm).cal', 'Cal 2022-01-24 14.52.10 (3DErr 0.352 mm).cal', 'Cal 2022-02-10 09.42.10 (3DErr 0.447 mm).cal', 'Cal 2022-02-17 10.12.50 (3DErr 0.507 mm).cal', 'Cal 2022-02-21 10.19.46 (3DErr 0.390 mm).cal', 'Cal 2022-03-04 10.12.26 (3DErr 0.347 mm).cal', 'Cal 2022-03-07 14.40.24 (3DErr 0.431
mm).cal', 'Cal 2022-03-15 10.33.04 (3DErr 0.365 mm).cal', 'Cal 2022-03-16 10.46.15 (3DErr 0.366 mm).cal', 'Cal 2022-04-11 11.27.07 (3DErr 0.385 mm).cal', 'Cal 2022-04-12 09.17.23 (3DErr 0.355 mm).cal', 'Cal 2022-04-21 15.25.11 (3DErr 0.353 mm).cal', 'Cal 2022-04-26 13.45.35 (3DErr 0.386 mm).cal', 'Cal 2022-05-16 17.21.05 (3DErr 0.397 mm).cal', 'Cal 2022-05-16 18.34.51
(3DErr 0.395 mm).cal', 'Cal 2022-05-23 14.05.07 (3DErr 0.375 mm).cal', 'Cal 2022-06-23 15.27.01 (3DErr 0.456 mm).cal', 'Cal 2022-07-05 14.15.08 (3DErr 1.125 mm) Excellent.cal', 'Cal 2022-07-05 14.18.10 (3DErr 0.487 mm).cal', 'Cal 2022-08-08 11.57.43 (3DErr 1.466 mm) Great.cal', 'Cal 2022-08-08 12.00.56 (3DErr 0.483 mm).cal', 'Cal 2022-08-23 14.11.53 (3DErr 0.457 mm).cal')
calib_dates=['2021-10-11 11.35.52', '2021-10-11 11.44.49', '2021-10-28 09.01.21', '2021-11-04 09.54.00', '2021-12-16 10.30.59', '2021-12-16 13.47.08', '2022-01-12 15.50.51', '2022-01-17 16.43.21', '2022-01-19 09.41.54', '2022-01-24 14.52.10', '2022-02-10 09.42.10', '2022-02-17 10.12.50', '2022-02-21 10.19.46', '2022-03-04 10.12.26', '2022-03-07 14.40.24', '2022-03-15
10.33.04', '2022-03-16 10.46.15', '2022-04-11 11.27.07', '2022-04-12 09.17.23', '2022-04-21 15.25.11', '2022-04-26 13.45.35', '2022-05-16 17.21.05', '2022-05-16 18.34.51', '2022-05-23 14.05.07', '2022-06-23 15.27.01', '2022-07-05 14.15.08', '2022-07-05 14.18.10', '2022-08-08 11.57.43', '2022-08-08 12.00.56', '2022-08-23 14.11.53']
calib_datetimes=
calib_files are the filenames, calib_dates are the extracted date strings, and calib_datetimes should be the list of datetimes.
Hope you guys can help!
Thanks!
CodePudding user response:
I have tried your code and it works
output:
calib_dates=['2021-10-11 11.35.52', '2021-10-11 11.44.49', '2021-10-28 09.01.21', '2021-11-04 09.54.00', '2021-12-16 10.30.59', '2021-12-16 13.47.08', '2022-01-12 15.50.51', '2022-01-17 16.43.21', '2022-01-19 09.41.54', '2022-01-24 14.52.10', '2022-02-10 09.42.10', '2022-02-17 10.12.50', '2022-02-21 10.19.46', '2022-03-04 10.12.26', '2022-03-07 14.40.24', '2022-03-15 10.33.04', '2022-03-16 10.46.15', '2022-04-11 11.27.07', '2022-04-12 09.17.23', '2022-04-21 15.25.11', '2022-04-26 13.45.35', '2022-05-16 17.21.05', '2022-05-16 18.34.51', '2022-05-23 14.05.07', '2022-06-23 15.27.01', '2022-07-05 14.15.08', '2022-07-05 14.18.10', '2022-08-08 11.57.43', '2022-08-08 12.00.56', '2022-08-23 14.11.53']
calib_datetimes=[datetime.datetime(2021, 10, 11, 11, 35, 52), datetime.datetime(2021, 10, 11, 11, 44, 49), datetime.datetime(2021, 10, 28, 9, 1, 21), datetime.datetime(2021, 11, 4, 9, 54), datetime.datetime(2021, 12, 16, 10, 30, 59), datetime.datetime(2021, 12, 16, 13, 47, 8), datetime.datetime(2022, 1, 12, 15, 50, 51), datetime.datetime(2022, 1, 17, 16, 43, 21), datetime.datetime(2022, 1, 19, 9, 41, 54), datetime.datetime(2022, 1, 24, 14, 52, 10), datetime.datetime(2022, 2, 10, 9, 42, 10), datetime.datetime(2022, 2, 17, 10, 12, 50), datetime.datetime(2022, 2, 21, 10, 19, 46), datetime.datetime(2022, 3, 4, 10, 12, 26), datetime.datetime(2022, 3, 7, 14, 40, 24), datetime.datetime(2022, 3, 15, 10, 33, 4), datetime.datetime(2022, 3, 16, 10, 46, 15), datetime.datetime(2022, 4, 11, 11, 27, 7), datetime.datetime(2022, 4, 12, 9, 17, 23), datetime.datetime(2022, 4, 21, 15, 25, 11), datetime.datetime(2022, 4, 26, 13, 45, 35), datetime.datetime(2022, 5, 16, 17, 21, 5), datetime.datetime(2022, 5, 16, 18, 34, 51), datetime.datetime(2022, 5, 23, 14, 5, 7), datetime.datetime(2022, 6, 23, 15, 27, 1), datetime.datetime(2022, 7, 5, 14, 15, 8), datetime.datetime(2022, 7, 5, 14, 18, 10), datetime.datetime(2022, 8, 8, 11, 57, 43), datetime.datetime(2022, 8, 8, 12, 0, 56), datetime.datetime(2022, 8, 23, 14, 11, 53)]
CodePudding user response:
The only issue I got is from f-strings. And that had to do with Google Colab:
The below code actually works and it is printing the data inside calib_datetimes
:
from datetime import datetime
calib_files = ('Cal 2021-10-11 11.35.52 (3DErr 4.949 mm) Poor.cal', 'Cal 2021-10-11 11.44.49 (3DErr 0.285 mm).cal', 'Cal 2021-10-28 09.01.21 (3DErr 0.656 mm) Excellent.cal', 'Cal 2021-11-04 09.54.00 (3DErr 0.336 mm).cal', 'Cal 2021-12-16 10.30.59 (3DErr 0.319 mm).cal', 'Cal 2021-12-16 13.47.08 (3DErr 0.350 mm).cal', 'Cal 2022-01-12 15.50.51 (3DErr 0.302 mm).cal', 'Cal 2022-01-17 16.43.21 (3DErr 0.239 mm).cal', 'Cal 2022-01-19 09.41.54 (3DErr 0.300 mm).cal', 'Cal 2022-01-24 14.52.10 (3DErr 0.352 mm).cal', 'Cal 2022-02-10 09.42.10 (3DErr 0.447 mm).cal', 'Cal 2022-02-17 10.12.50 (3DErr 0.507 mm).cal', 'Cal 2022-02-21 10.19.46 (3DErr 0.390 mm).cal', 'Cal 2022-03-04 10.12.26 (3DErr 0.347 mm).cal', 'Cal 2022-03-07 14.40.24 (3DErr 0.431 mm).cal', 'Cal 2022-03-15 10.33.04 (3DErr 0.365 mm).cal', 'Cal 2022-03-16 10.46.15 (3DErr 0.366 mm).cal', 'Cal 2022-04-11 11.27.07 (3DErr 0.385 mm).cal', 'Cal 2022-04-12 09.17.23 (3DErr 0.355 mm).cal', 'Cal 2022-04-21 15.25.11 (3DErr 0.353 mm).cal', 'Cal 2022-04-26 13.45.35 (3DErr 0.386 mm).cal', 'Cal 2022-05-16 17.21.05 (3DErr 0.397 mm).cal', 'Cal 2022-05-16 18.34.51 (3DErr 0.395 mm).cal', 'Cal 2022-05-23 14.05.07 (3DErr 0.375 mm).cal', 'Cal 2022-06-23 15.27.01 (3DErr 0.456 mm).cal', 'Cal 2022-07-05 14.15.08 (3DErr 1.125 mm) Excellent.cal', 'Cal 2022-07-05 14.18.10 (3DErr 0.487 mm).cal', 'Cal 2022-08-08 11.57.43 (3DErr 1.466 mm) Great.cal', 'Cal 2022-08-08 12.00.56 (3DErr 0.483 mm).cal', 'Cal 2022-08-23 14.11.53 (3DErr 0.457 mm).cal')
format = '%Y-%m-%d %H.%M.%S'
calib_datetimes = []
calib_dates = []
for calib in calib_files:
calib_date = ' '.join(calib.split(' ')[1:3])
calib_dates.append(calib_date)
calib_datetimes.append(datetime.strptime(calib_date, format))
print(f'{calib_dates}')
print(f'{calib_datetimes}')