-3

I'm trying to delete specific list from a text file and overwrite the original text file without using os.replace. However,i tried using truncate but nothing happen to my txt file..

Input:

def delete_tenants():
    a = []
    tnf = False

    # while not found:
    with open("tenant_application.txt", "r") as file:
        lines = file.readlines()

    with open("tenant_application.txt", "r") as file:
        for line in file:
            a = line.split(",")  # split lines using comma
            print(a)  # display all the data in txt file

    with open("tenant_application.txt", "r") as file:
        delete = input("Enter the primary key to delete: ")
        for line in file:
            a = line.split(",")  # split lines using comma
            b = a[10].strip()
            c = delete.strip()
            d = c == b
            if d:
                print("Line that you want to delete:", line)
                tnf = True
                break

    if tnf == False:
        print("\nInvalid Input\n")
        return delete_tenants()

    if tnf == True:
        input1 = "tenant_application.txt"
        #
        with open(input1, "r+") as file1:
            lines = file1.readlines()
            file1.seek(0)
            for i in lines:
                if i not in a:
                    file1.write(i)
            file1.truncate()

My text file:

Celine ,43,Female,Selangor,Kuala Lumpur,Happy Garden,Persiaran Sungai Long 2 ,2003-04-03 00:00:00,43000,111,0001
Jackson ,43,Female,Selangor,Kuala Lumpur,Happy Garden,Persiaran Sungai Long 2 ,2003-04-03 00:00:00,43000,111,0002
Wong ,43,Female,Selangor,Kuala Lumpur,Happy Garden,Persiaran Sungai Long 2 ,2003-04-03 00:00:00,43000,111,0003
Liew ,43,Female,Selangor,Kuala Lumpur,Happy Garden,Persiaran Sungai Long 2 ,2003-04-03 00:00:00,43000,111,0004
Monchi Yui
  • 11
  • 2
  • This is a CSV file. Python can read and parse CSV file through the `csv` module but an even better option is to use Pandas to read the file, remove the row you want and then save it back. This can be done in 3-4 lines. What are you *actually* trying to do? How are you trying to identify the lines to delete? – Panagiotis Kanavos Sep 08 '22 at 14:24

1 Answers1

0

This is a CSV file. Python can read and parse CSV file through the [csv module] but an even better option is to use Pandas to read the file, remove the row you want and then save it back. This can be done in just a few lines.

You can read a CSV into a DataFrame (roughly an in-memory table) using the pandas.read_csv function:

import pandas as pd

df=pd.read_csv(path,header=None)

If the file has a header, the dataframe's columns will use the header names. If not, the column names will be numbers.

Given the question's example

import pandas as pd
from io import StringIO

csv="""Celine ,43,Female,Selangor,Kuala Lumpur,Happy Garden,Persiaran Sungai Long 2 ,2003-04-03 00:00:00,43000,111,0001
Jackson ,43,Female,Selangor,Kuala Lumpur,Happy Garden,Persiaran Sungai Long 2 ,2003-04-03 00:00:00,43000,111,0002
Wong ,43,Female,Selangor,Kuala Lumpur,Happy Garden,Persiaran Sungai Long 2 ,2003-04-03 00:00:00,43000,111,0003
Liew ,43,Female,Selangor,Kuala Lumpur,Happy Garden,Persiaran Sungai Long 2 ,2003-04-03 00:00:00,43000,111,0004"""

df=pd.read_csv(StringIO(csv),header=None)

The first column values can be retrieved with df[0]:

df[0]
----------------
0     Celine
1    Jackson
2       Wong
3       Liew

Instead of deleting a row based on some condition, we can filter them to exclude the rows that don't match the condition:

df=df[df[10]!=4]
df[0]
---------------
0     Celine
1    Jackson
2       Wong

Once we have the dataframe we want we can save it with to_csv :

df.to_csv(path,header=False,index=false)

It's possible to manipulate string columns to eg strip them of whitespace :

df[0]=df[0].str.strip()

The answers to this SO question shows various ways to strip all string columns, eg :

df=df.applymap(lambda x: x.strip() if isinstance(x, str) else x)

Putting all this together:

import pandas as pd

df=pd.read_csv(path,header=None)
id= input("Enter the primary key to delete: ")
df=df[df[10]!=id]
df=df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
df.to_csv(path,header=False,index=false)
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236