0

The question here is related to the same type of file I asked another question about, almost one month ago (I need to split a seismological file so that I have multiple subfiles).

My goal now is to delete events which in their first line contain the string 'RSN 3'. So far I have tried editing the aforementioned question's best answer code like this:

with open(refcatname) as fileContent:
    for l in fileContent:
        check_rsn_3 = l[45:51]
        if check_rsn_3 == "RSN  3":
            line = l[:-1]
            check_event = line[1:15]
            print(check_event, check_rsn_3)
        if not check_rsn_3 == "RSN  3":
            # Strip white spaces to make sure is an empty line
            if not l.strip():
                subFile.write(
                    eventInfo + "\n"
                )  # Add event to the subfile
                eventInfo = ""  # Reinit event info
                eventCounter += 1
                if eventCounter == 700:
                    subFile.close()
                    fileId += 1
                    subFile = open(
                        os.path.join(
                            catdir,
                            "Paquete_Continental_"
                            + str(fileId)
                            + ".out",
                        ),
                        "w+",
                    )
                    eventCounter = 0
            else:
                eventInfo += l
subFile.close()

Expected results: Event info of earthquakes with 'RSN N' (where N≠3) Actual results: First line of events with 'RSN 3' is deleted but not the remaining event info.

Thanks in advance for your help :)

1 Answer 1

0

I'd advise against checking if the string is at an exact location (e.g. l[45:51]) since a single character can mess that up, you can instead check if the entire string contains "RSN 3" with if "RSN 3" in l

With the line = l[:-1] you only get the last character of the line, so the line[1:15] won't work since it's not an array.

But if you need to delete several lines, you could just check if the current line contains "RSN 3", and then read line after line until one contains "RSN " while skipping the ones in between.

skip = False
for line in fileContent:
    if "RSN  3" in line:
        skip = True
        continue
    if "RSN  " in line and "RSN  3" not in line:
        skip = False
        # rest of the logic
    if skip:
        continue

This way you don't even parse the blocks whose first line contains "RSN 3".

4
  • Hi, I didn't have time to check your answer. It seems as a smart approach, just one question...by rest of the logic do you mean to append and write the file? Commented Apr 11, 2019 at 12:20
  • And when you say-> "RSN " in line and "RSN 3" not in line, is it skiping events with (e.g.) "RSN 8"? Commented Apr 11, 2019 at 12:43
  • Yes, you could add those lines to a different array and then process just them afterwards, that way this whole loop is just a filter. Think about it, does a line that contains RSN 8 match both of those conditions? Does it contain RSN, but doesn't contain RSN 3? :) Commented Apr 12, 2019 at 9:39
  • Makes sense, I actually found one way editing your answer. Still thanks a lot! Commented Apr 15, 2019 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.