Oct-28-2022, 10:23 AM
(This post was last modified: Oct-28-2022, 11:41 AM by rob101.
Edit Reason: added a possible solution
)
(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)
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
>>> 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