Python Forum
Given a date I need to separate data into weeks
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Given a date I need to separate data into weeks
#13
Thank You larz60 I will try your solution
In the mean time can someone explain to me why this might be happening?
First why does
values.strip()
not take off the /n but
cleanedData.append(datetime.strptime(re.sub(',+|\\r|\\n|','', values), '%d/%m/%Y %H:%M'))
does?

Second why is the date 4/4/2017 12:57 in the proper format for the line
cleanedData.append(datetime.strptime(re.sub(',+|\\r|\\n|','', values), '%d/%m/%Y %H:%M'))
but '10/20/2016 11:28' is not?

As you can see from the output I have been able to separate the header 'created' from the dates that follows by adding the \n to the word in the if compare statement. When I tried to strip off the \n and do the compare it did not work, why?

What is different about the dates 4/4/2017 12:57 and 10/20/2016 11:28 that allows the format.strptime to process the first one but complain that the second one is not in the corrrect format?

Output:
Before the if else block created in the if statement created Before the if else block 4/4/2017 12:57 in the else statement 4/4/2017 12:57 Added to dictionary key [' c r e a t e d ', datetime.datetime(2017, 4, 4, 12, 57)] Before the if else block 10/20/2016 11:28 in the else statement 10/20/2016 11:28 Traceback (most recent call last): File "date_filter_error_report.py", line 211, in <module> parsedData = cleanTheData(parsedData) File "date_filter_error_report.py", line 113, in cleanTheData cleanedData.append(datetime.strptime(re.sub(',+|\\r|\\n|','', values), '%d/%m/%Y %H:%M')) File "C:\Python36\lib\_strptime.py", line 565, in _strptime_datetime tt, fraction = _strptime(data_string, format) File "C:\Python36\lib\_strptime.py", line 362, in _strptime (data_string, format)) ValueError: time data '10/20/2016 11:28' does not match format '%d/%m/%Y %H:%M'
def cleanTheData(ds):

    # removes excess characters from sys_created_on values
    cleanedData = list() # clears list
    for values in ds['sys_created_on']:
        print('Before the if else block', values)
        if values=='created\n':
            cleanedData.append(re.sub(',+|\\r|\\n|', ' ', values))
            print('in the if statement', values)
        else:
            print('in the else statement', values)
            cleanedData.append(datetime.strptime(re.sub(',+|\\r|\\n|','', values), '%d/%m/%Y %H:%M'))
            ds['sys_created_on'] = cleanedData
            print('Added to dictionary key', ds['sys_created_on'])

    return ds
Reply


Messages In This Thread
RE: Given a date I need to separate data into weeks - by cspower - Sep-06-2017, 08:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 411 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 842 Jan-20-2024, 04:45 AM
Last Post: 1418
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,508 May-17-2022, 07:49 AM
Last Post: Pedroski55
  Date format and past date check function Turtle 5 4,613 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,330 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  How to add date and years(integer) to get a date NG0824 4 3,006 Sep-03-2020, 02:25 PM
Last Post: NG0824
  Pulling & Reading Date from UDF that Compare it to Live Data firebird 4 2,933 Jul-20-2019, 09:30 AM
Last Post: snippsat
  Substracting today's date from a date in column of dates to get an integer value firebird 1 2,212 Jul-04-2019, 06:54 PM
Last Post: Axel_Erfurt
  How to change existing date to current date in a filename? shankar455 1 2,384 Apr-17-2019, 01:53 PM
Last Post: snippsat
  Date format conversion "/Date(158889600000)/" lbitten 2 2,943 Nov-29-2018, 02:14 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020