Posts: 122
Threads: 24
Joined: Dec 2017
(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.
Posts: 122
Threads: 24
Joined: Dec 2017
(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: ''
Posts: 453
Threads: 16
Joined: Jun 2022
(Oct-28-2022, 10:34 PM)jehoshua Wrote: Thanks, I added a loop to read the file, code now ..
Again, you're welcome.
If nothing else in this thread is working for you and you'd like to implement my solution, then I'll need some more information: limited information will produce limited results.
As yet, the only information I have about what is being returned by lines = f.readlines() is what you've posted: a list object such as ['D6', '30', '2005\n'] or ['D7', '7', '2005\n'] . I'm guessing that the list object contains more than that.
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
Posts: 122
Threads: 24
Joined: Dec 2017
(Oct-28-2022, 12:53 PM)DeaD_EyE Wrote: import datetime
input_format = "D%d/%m'%Y"
output_format = "%d/%m/%Y"
datetime.datetime.strptime("D4/7'2004", input_format).strftime(output_format)
Thank you. I added a bit of code to read the file as follows ..
import datetime
input_format = "D%d/%m'%Y"
output_format = "%d/%m/%Y"
f = open("ANCU.qif","r")
lines = f.readlines()
for line in lines:
if line[0] == "D":
new_date = datetime.datetime.strptime(line.strip(), input_format).strftime(output_format)
print(new_date)
else:
line = line.strip()
print(line)
# closing the file
f.close() and it worked for 34 records, then ..
Output: Traceback (most recent call last):
File "....search_replace_6.py", line 12, in <module>
new_date = datetime.datetime.strptime(line.strip(), input_format).strftime(output_format)
File "/usr/lib/python3.10/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/usr/lib/python3.10/_strptime.py", line 349, in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data "D3/29'2004" does not match format "D%d/%m'%Y"
of course line 12 is the line I modified. However, the previous date records were okay because they were all
D3/11'2004
and this date record was
D3/29'2004
so the code thought the month was 29
Posts: 122
Threads: 24
Joined: Dec 2017
(Oct-28-2022, 10:59 PM)rob101 Wrote: I'm guessing that the list object contains more than that.
Yes, I'll create some test data. Thanks
Posts: 2,122
Threads: 10
Joined: May 2017
(Oct-28-2022, 11:20 PM)jehoshua Wrote: so the code thought the month was 29
The input_format was wrong.
input_format = "D%d/%m'%Y" -> day/month/year
But it seems to be month/day/year
This should work
input_format = "D%m/%d'%Y" More info about the shortcuts: https://docs.python.org/3/library/dateti...rmat-codes
Posts: 122
Threads: 24
Joined: Dec 2017
(Oct-29-2022, 03:10 PM)DeaD_EyE Wrote: This should work
Thanks, yes it works fine now as:
import datetime
input_format = "D%m/%d'%Y"
output_format = "D%m/%d/%Y"
f = open("ANCU.qif","r")
lines = f.readlines()
for line in lines:
if line[0] == "D":
new_date = datetime.datetime.strptime(line.strip(), input_format).strftime(output_format)
print(new_date)
else:
line = line.strip()
print(line)
# closing the file
f.close()
Posts: 122
Threads: 24
Joined: Dec 2017
Oct-29-2022, 08:44 PM
(This post was last modified: Oct-29-2022, 08:45 PM by jehoshua.)
(Oct-28-2022, 11:23 PM)jehoshua Wrote: Yes, I'll create some test data. Thanks
Some test data at https://pastebin.com/raw/FnueXARj
|