0

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.

2 Answers2

0

Can you check your code with this modification?

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 = fline.replace("Mon,", " ")  
    fline = fline.replace('Tue,', " ")  
    fline = fline.replace('Wed,', " ")  
    fline = fline.replace('Thu,', " ")  
    fline = fline.replace('Fri,', " ")  
    fline = fline.replace('Sat,', " ")  
    fline = fline.replace('Sun,', " ")  
   print(type(fline),fline)
Happy Ahmad
  • 1,072
  • 2
  • 14
  • 33
0

I am making some presumptions about the nature of the problem you are attempting to solve, so correct me if I get something wrong.

Presuming you want to replace all instances of "DDD, " (note the extra space) with nothing,
you may want to search for slightly modified strings by adding the extra space... more about this later.

In the snippet below, I added some comments inline to either confirm my understanding OR to clarify what might be awry. A recommendation follows at the end.

weekday = ('Mon,', 'Tue,', 'Wed,', 'Thu,', 'Fri,', 'Sat,', 'Sun,')   

with open(inf, "r") as infile:  
    with open(outf, "w") as outfile:  

        # If you intend to call the .write() method, it will need an argument
        # so that the function knows what to write.
        # At this point, we don't seem to have anything to write, yet.
        # I am presuming this next line is out of place.
        oline = outfile.write()  

        for fline in infile:  

            # weekday is a tuple. It has strings in it, but it is a tuple. 
            # As such the text in fline will never start with it.
            # I feel like you want to cycle through each weekday in weekdays
            # and if you find that weekday, you simply want to replace that
            # snippet of text, if it is found.
            # We will need a different approach, more on this later.

            if fline.startswith(weekday):  
            
            # It should be noted that .replace() does not do an "in place"
            # replacement. If we want to save the results of the replacement, 
            # we need to set a variable to point at the result. 
            # Options include either of the following:
            # fline = fline.replace("Mon,", " ") if you just want the new data
            # newline = fline.replace("Mon,", " ") if you want to keep fline
            # untouched for later reference    
                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)

We might rewrite the code in this way.

Again, presuming I understand your end goal:

inf = "/Users/dusti/Documents/datetime25.txt"  
outf = "/Users/dusti/Documents/datetime30.txt"  

# We could add a blank space after each string in weekdays AND 
# give it the variable name weekdays to better reflect that it is
# a sequence of individual weekdays.

weekdays = ('Mon, ', 'Tue, ', 'Wed, ', 'Thu, ', 'Fri, ', 'Sat, ', 'Sun, ')   

with open(inf, "r") as infile:  
    with open(outf, "w") as outfile:  
        for fline in infile:  

            # We could parse each weekday in weekdays
            for weekday in weekdays:
                
                # For each weekday in weekdays, we could then
                # do a replacement of that weekday, if it exists
                # I am presuming that we don't want extraneous blank spaces 
                # To prevent "Mon, 11 May 2026 05:41:27" from becoming:
                #            "  11 May 2026 05:41:27"
                # 
                # We use our tuple ("Mon, ", "Tue, "...) with the slightly
                # enhanced "space-included" strings AND then we replace the
                # term with an empty string will ensure that the value comes out
                # as this: "11 May 2026 05:41:27" with no extraneous spaces.
 
                fline = fline.replace(weekday, "") # NOTE the empty string (`""`)
                                                   # as a replacement value
            
            # NOTE: this line is outdented from the for loop, cause
            # we only want to write to the outfile when we have
            # finished checking against each weekday in weekdays and have a 
            # final result to write.
            outfile.write(fline)
E. Ducateme
  • 4,028
  • 2
  • 20
  • 30
  • So this solved the issue! if anyone else reads this thread I had a small issue where it was only filtering out one piece of data instead of many and I just needed to indent the ```outfile.write(fline)``` slightly further! Thanks :) – NeetoBurrito Feb 02 '22 at 01:17