Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modifying a date format
#12
(Oct-28-2022, 10:23 AM)rob101 Wrote: As I had 10 mins spare, I've come up with a possible solution:

Thanks, I added a loop to read the file, code now ..

def format_date(date):
    for index, item in enumerate(date):
        if index == 0:
            month = item.lstrip("D")
            month = "D" + str(f"{int(month):02d}")
        elif index == 1:
            day = str(f"{int(item):02d}")
        else:
            year = item.rstrip("\n")

    return f"{month}/{day}/{year}"

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

for line in lines:
    if line[0] == 'D':
        date = format_date(line)
    continue

print(date)

# closing the file
f.close()
Output:
Traceback (most recent call last): File "......search_replace_5.py", line 18, in <module> date = format_date(line) File ".....search_replace_5.py", line 5, in format_date month = "D" + str(f"{int(month):02d}") ValueError: invalid literal for int() with base 10: ''
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 279 May-22-2024, 07:04 PM
Last Post: deanhystad
  Compare current date on calendar with date format file name Fioravanti 1 470 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 905 Jan-20-2024, 04:45 AM
Last Post: 1418
  Date format error getting weekday value Aggie64 2 1,540 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,083 Feb-09-2022, 04:44 PM
Last Post: Racer_x
  How can I compare 2 format of date? korenron 4 1,674 Dec-21-2021, 12:40 PM
Last Post: korenron
  Date format and past date check function Turtle 5 4,714 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Print first day of the week as string in date format MyerzzD 2 2,152 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  String to Date format SAF 2 2,581 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