0

I have a text file from which I want to delete all data up to the point where I see the value 'NODATACODE' . The text in the text file is:

MMMMM ; MMMMM : MMMMMMMMMMN, AAAAAAAAAAA,52, AAAA,CCCCCC, MMMMM ; MMMMM : MMMMMMMMMMN, 
  >AAAAAAAAAAA,200, AAAA,CCCCCC,;MMMMM ; MMMMM : MMMMMMMMMMN, AAAAAAAAAAA,53, 
  >AAAA,CCCCCC,AAAA AAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAAAAAA NODATACODE, : Food Meal

Please let me know how I can rewrite the following code in Python to perform this task. I tried the following code but it doesn't work:

with open('Schedule.txt', 'w') as fw:
   for line in lines:
   if line.strip('\n') = 'NODATACODE':
                      fw.write(line)

Error message that I get is below:

     Cell In[1], line 5
     if line.strip('\n') = 'NODATACODE':
        ^
     SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of 
       '='?

Original Output

enter image description here

Desired Output

enter image description here

Thank you in advance.

6
  • 1
    Did you read the error message? Commented Dec 25, 2023 at 8:53
  • if line.strip('\n') == 'NODATACODE': Commented Dec 25, 2023 at 8:55
  • 2
    Line 5 should be !=, but this is a wild guess since your question is not clear enough. In that file are those lines separated by line breaks? Are there more lines after "NODATACODE"? The indentation is wrong. And I think you might need a read handle to get all the lines first, close it and write handle to write the lines you want. Commented Dec 25, 2023 at 8:56
  • @AnalysisNerd, can you make a meaningful example and show the exact matching expected output ? Commented Dec 25, 2023 at 9:00
  • @Timeless. Just making the required edits to my question. Thank you for your patience. Commented Dec 25, 2023 at 9:03

1 Answer 1

0

This should do what you want; note that we test whether the line begins with 'NODATACODE', not is equal to it. And we use a flag so that the next lines will be written to the output file too:

with open('input_file.txt') as f_in:
    with open('output_file.txt', 'w') as f_out:
        write_flag = False
        for line in f_in.readlines():
            if line.startswith('NODATACODE'):
                write_flag = True
            if write_flag:
                f_out.write(line)

If 'NODATACODE' is likely to be inside a line, an approach with regex could be better:

import re

with open('input_file.txt') as f_in:
    with open('output_file.txt', 'w') as f_out:
        data = f_in.read()
        f_out.write(re.sub(r'[\w\W]*NODATACODE', 'NODATACODE', data))
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @swifty! I will try this piece of code promptly. I was just wondering if this 'NOTDATACODE' is in the middle of a line is there a way of deleting all data before it?
If NODATACODE is likely to be in the middle of the line, perhaps another approach using a regexp is in order.
Thank you @Swifty. That is a new approach I will try out.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.