Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modifying a date format
#1
I have some QIF data in a file, and the 'Date" record needs modifying. The Dates are of the format

D4/7'2004

and they need to be changed to

D04/07/2004

f = open("ANCU.qif","r")
lines = f.readlines()

for line in lines:
    first_char = line[0]

    if(first_char == "D"):
        print("Found a date record")
        print(first_char)
        new_line = line.replace("'","/")
        parts = new_line.split("/",2)
        print(parts)
        new = "/".join(parts)
        print(new)
    else:
        print("Nope")
        print(first_char)

# closing the file
f.close()
is producing ..

Quote:Found a date record
D
['D6', '30', '2005\n']
D6/30/2005

Nope
C
Nope
T
Nope
P
Nope
L
Nope
^
Found a date record
D
['D7', '7', '2005\n']
D7/7/2005

Nope
C
Nope
M
Nope
T
Nope
P
Nope
L
Nope
^

so the code is nearly there. The dates are in MM/DD/YYYY format. The rules for the code need to be if the MM or DD is a length of one char, then add the leading zero. Also I assume the \n is a carriage return or line feed to indicate a record separation.

I also need to write a new file with only the date records modified, so that needs to be added in wherever the print statements are, or similar.
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
Exclamation Date format convert problem for SQL server eotret 5 2,327 Aug-09-2024, 07:19 AM
Last Post: Pedroski55
  Invalid Date Format fo Cached Files jland47 1 1,137 May-22-2024, 07:04 PM
Last Post: deanhystad
  Compare current date on calendar with date format file name Fioravanti 1 2,248 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 2,922 Jan-20-2024, 04:45 AM
Last Post: 1418
  Date format error getting weekday value Aggie64 2 2,163 May-29-2022, 07:04 PM
Last Post: Aggie64
  Convert Date to another format lonesoac0 2 2,512 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Format SAS DATE Racer_x 0 1,582 Feb-09-2022, 04:44 PM
Last Post: Racer_x
  How can I compare 2 format of date? korenron 4 2,431 Dec-21-2021, 12:40 PM
Last Post: korenron
  Date format and past date check function Turtle 5 10,698 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Print first day of the week as string in date format MyerzzD 2 2,932 Sep-29-2021, 06:43 AM
Last Post: MyerzzD

Forum Jump:

User Panel Messages

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