Search code examples
pythontexttext-filespython-3.11

How to delete text in text file using Python based on conditions?


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.


Solution

  • Here's a revised version of your script:

    # Read the file content first
    with open('Schedule.txt', 'r') as file:
        data = file.read()
    
    # Find the index where 'NODATACODE' occurs
    nodata_index = data.find('NODATACODE')
    
    # Check if 'NODATACODE' is found
    if nodata_index != -1:
        # Extract the text from 'NODATACODE' to the end
        data_to_write = data[nodata_index:]
    
        # Write the modified data back to the file
        with open('Schedule.txt', 'w') as file:
            file.write(data_to_write)
    else:
        print("'NODATACODE' not found in the file.")
    

    In this script:

    1. The file is first opened in read mode to get all its content.
    2. We find the index of the first occurrence of 'NODATACODE' in the file's data.
    3. If 'NODATACODE' is found, we extract all the text from this point onwards.
    4. Finally, we open the file in write mode and overwrite it with the extracted text.

    This script assumes that 'NODATACODE' appears only once in your file. If 'NODATACODE' can appear multiple times and you want to delete content up to the last occurrence, you would need to adjust the logic to find the last index of 'NODATACODE'.

    Remember, opening a file in write mode ('w') as you did initially will immediately truncate the file, so it's important to read its content before overwriting it. The syntax error you encountered is also fixed in this revised script.