python - Delete lines from text file using regex sub -
I have a text file that has lines like this:
fruit = apple id = = 1001 = weight = 7 colors = red ... fruit = watermelon id = # 1002 weight = 20 color = green ... fruit = cherry id = # 1003 ... I Trying to remove all the rows related to the fruit to remove an id of the fruit. So, I read in # 1002 and I want to delete all the lines from fruit = melon (but not included in this) fruit = cherry . I do not know how much information each fruit has and they will be different. I have tried to use regex via the following logic:
repl_string = "fruit = (. *?) \ NId =" + user_inputted_id_to_match + " \ N (*. * Basically, I'm matching the line with the function , which is id line that user Gives me everything and then brings everything to a lookhead for the next fruit line, does this make sense? I ran and only in the resulting text file The value of Id was deleted Is: fruit = Apple id = # 1001 weight = 7 colors = red ... fruit = watermelon id = weight = 20 color = green ... fruit = cherry id = # 1003. .. How do I delete? All the lines corresponding to a given result?
Comments
Post a Comment