Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modifying a date format
#11
(Oct-28-2022, 08:58 AM)Larz60+ Wrote: You don't need testit.
try this (as I cannot test):

Thanks, I only had to modify line 5 as it didn't like double quotes within the "F" statement, so ..

import datetime

def convert_date(orig):
    massaged_date = orig.replace("'", "/")[1:]
    return f"D{datetime.datetime.strftime(datetime.datetime.strptime(massaged_date, '%m/%d/%Y'), '%m/%d/%Y')}"

def clean_file(filename):
    with open(filename,"r") as f:
        line = f.readline().strip()

        if line[0] == "D":      # this could also read "if line.startswith("D"):"
            convert_date(line)
            print(line)
        else:
            print(line)

clean_file("ANCU.qif")
it only read one line, so I added a "for" loop but that didn't work.
Reply


Messages In This Thread
Modifying a date format - by jehoshua - Oct-27-2022, 08:14 PM
RE: Modifying a date format - by deanhystad - Oct-27-2022, 08:53 PM
RE: Modifying a date format - by jehoshua - Oct-27-2022, 09:34 PM
RE: Modifying a date format - by rob101 - Oct-27-2022, 08:59 PM
RE: Modifying a date format - by jehoshua - Oct-28-2022, 03:42 AM
RE: Modifying a date format - by Larz60+ - Oct-27-2022, 09:57 PM
RE: Modifying a date format - by jehoshua - Oct-28-2022, 03:56 AM
RE: Modifying a date format - by Larz60+ - Oct-28-2022, 08:58 AM
RE: Modifying a date format - by jehoshua - Oct-28-2022, 10:17 PM
RE: Modifying a date format - by rob101 - Oct-28-2022, 10:23 AM
RE: Modifying a date format - by jehoshua - Oct-28-2022, 10:34 PM
RE: Modifying a date format - by DeaD_EyE - Oct-28-2022, 12:53 PM
RE: Modifying a date format - by jehoshua - Oct-28-2022, 11:20 PM
RE: Modifying a date format - by rob101 - Oct-28-2022, 10:59 PM
RE: Modifying a date format - by jehoshua - Oct-28-2022, 11:23 PM
RE: Modifying a date format - by jehoshua - Oct-29-2022, 08:44 PM
RE: Modifying a date format - by DeaD_EyE - Oct-29-2022, 03:10 PM
RE: Modifying a date format - by jehoshua - Oct-29-2022, 08:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Invalid Date Format fo Cached Files jland47 1 277 May-22-2024, 07:04 PM
Last Post: deanhystad
  Compare current date on calendar with date format file name Fioravanti 1 463 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 900 Jan-20-2024, 04:45 AM
Last Post: 1418
  Date format error getting weekday value Aggie64 2 1,538 May-29-2022, 07:04 PM
Last Post: Aggie64
  Convert Date to another format lonesoac0 2 1,794 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Format SAS DATE Racer_x 0 1,079 Feb-09-2022, 04:44 PM
Last Post: Racer_x
  How can I compare 2 format of date? korenron 4 1,672 Dec-21-2021, 12:40 PM
Last Post: korenron
  Date format and past date check function Turtle 5 4,703 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Print first day of the week as string in date format MyerzzD 2 2,148 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  String to Date format SAF 2 2,569 Apr-06-2021, 02:09 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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