I'm working on a puzzle for python comprehensibility and I'm currently trying to open a file, read it, replace some text, and then write that replaced text into a new file that's been made and specified. Right now as part of my solution to the puzzle I'm cleaning up datetime looking strings and trying to rearrange it and change the information to make organizing it later easier. The trouble I'm having is that when I try to replace text it doesn't work and just prints the filtered dates that start with the weekdays information.
I'm pretty sure this is because its trying to write on a file that i've specified as read only but when I replace 'fline.replace(...) with outfile.replace or oline.replace it tells me with pycharm that .replace can't be called in the class that outfline and oline are. So I'm a bit confused on how to call the correct file/variable to execute .replace. I can get it to work if I use this longer method but I'm trying to make it work with "with open" and a for loop to build up my for loop foundation. I've also tried including the with open(...) as outflile: in the for loop with the same issues.
inf = "/Users/dusti/Documents/datetime25.txt"
outf = "/Users/dusti/Documents/datetime30.txt"
weekday = ('Mon,', 'Tue,', 'Wed,', 'Thu,', 'Fri,', 'Sat,', 'Sun,')
with open(inf, "r") as infile:
with open(outf, "w") as outfile:
oline = outfile.write()
for fline in infile:
if fline.startswith(weekday):
fline.replace("Mon,", " ")
fline.replace('Tue,', " ")
fline.replace('Wed,', " ")
fline.replace('Thu,', " ")
fline.replace('Fri,', " ")
fline.replace('Sat,', " ")
fline.replace('Sun,', " ")
print(type(fline),fline)
Here's a small snippet of the information I'm looking through in '.datetiime25'. The new file will be written over every time I'm just trying to perfect whats going on before sorting all 1000 lines of info.
2013 02 08 22:52:57
2018-04-03T05:18:28.737971
Thu, 21 Dec 2023 11:35:04
2019-09-25T22:54:08.456561
Sat, 19 Apr 2025 01:49:18
2020 06 04 10:06:01
02/03/2021 02:47:55 PM
2024-01-27T08:48:32.559333
05/21/2014 05:31:48 PM
07/23/2014 12:07:21 AM
Mon, 11 May 2026 05:41:27
I've been looking at other questions similar to this one and I feel like there are ways to do this more elegantly with other modules involved and that .replace has more issues with it but as I'm very new to python I haven't been able to understand other modules methods.I want to understand what's going wrong here. I'm looking for information on why what I did didn't work and how to fix it so I can understand for future problems. Thanks in advance.