Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modifying a date format
#9
(Oct-28-2022, 03:42 AM)jehoshua Wrote: Thank you. I tried to incorporate the code you supplied and have it process the file...

You're welcome.

The code I posted is designed to iterate over the list object that is returned by lines = f.readlines(), but you've introduced another for loop, which unpacks said list, which is why if fails.

There are other solutions here which may very well work for you, but if not, I'm sure I could modify my script; indeed, I'm sure you could also do that, if needed.



To add...

As I had 10 mins spare, I've come up with a possible solution:

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}"


lines = ['D7', '7', '2005\n']  # a data sample

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

print(date)
jehoshua likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
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 1,943 Aug-09-2024, 07:19 AM
Last Post: Pedroski55
  Invalid Date Format fo Cached Files jland47 1 967 May-22-2024, 07:04 PM
Last Post: deanhystad
  Compare current date on calendar with date format file name Fioravanti 1 1,886 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 2,528 Jan-20-2024, 04:45 AM
Last Post: 1418
  Date format error getting weekday value Aggie64 2 2,055 May-29-2022, 07:04 PM
Last Post: Aggie64
  Convert Date to another format lonesoac0 2 2,374 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Format SAS DATE Racer_x 0 1,499 Feb-09-2022, 04:44 PM
Last Post: Racer_x
  How can I compare 2 format of date? korenron 4 2,286 Dec-21-2021, 12:40 PM
Last Post: korenron
  Date format and past date check function Turtle 5 9,587 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Print first day of the week as string in date format MyerzzD 2 2,772 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