1 Replies - 7265 Views - Last Post: 20 March 2013 - 04:48 PM Rate Topic: -----

#1 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Removing items from a text file

Posted 18 March 2013 - 03:32 PM

Been a while since I programmed, so I forgot my terminology. I am just wondering how to remove items from a text file, so there are two documents that I created.

Here is my source code
class Shop:
    def __init__(self, name="no name", color="no color", date="no date"):
        self.__name = name
        self.__color = color
        self.__date = date

    def setname(self, name):
        self.name = name
        return name

    def setcolor(self, color):
        self.color = color
        return color

    def setdate(self, date):
        self.date = date
        return date

    def getInfo(self):
        print("Name: ",self.__name)
        print("Color: ",self.__color)
        print("Date: ",self.__date + ("\n"))

ask_name, ask_color, ask_date = "", "", ""
counter, ask_user = 0, 0
while counter != 4:
    ask_user = int(input("Would you like add information?\n1. Yes\t\t2. Open File\t\t3. Exit"))
    while(True):
        if ask_user == 1:
            text_file = open("Text File.txt", "a")
            ask_name = input("Instruction: You must save, before storing a new information.\nWhat is your name?")
            ask_color = input("What is your favourite color?")
            ask_date = input("What is your favourite date?")
            text_file.write("\nName: %s\nColor: %s\nDate: %s\n\n" % (ask_name, ask_color, ask_date))
            text_file.close()
            print("You have a new Contact\nName:%s\nColor:%s\nDate:%s" % (ask_name, ask_color, ask_date))
            break

        if ask_user == 2:
            text_file = open("Text File.txt", "r")
            new_array = []
            new_array = text_file.read()
            print(new_array)
            text_file.close()
            break
        if ask_user == 3:
            print("\nYou saved your files successfully.")
            raise SystemExit


An empty text file:



A text file with info in it:
Name: John
Color: Yellow
Date: Wednesday


Output

Name: Josh
Color: Yellow
Date: Wednesday


Name: John
Color: Green
Date: Tuesday


Name: James
Color: Red
Date: Monday

Expected Output
Name: Josh
Color: Yellow
Date: Wednesday

Name: John
Color: Green
Date: Tuesday

Name: James
Color: Red
Date: Monday

So my major problem I having at the moment is with space issues, I cannot figure out a way to remove the excessive amount of blank lines left. In addition, I would like to know if there is a way to remove information from the text file such as

Expected Output
Name: Josh
Color: Yellow
Date: Wednesday

# Removing John's information out of the list.

Name: James
Color: Red
Date: Monday

I heard that pop() function is good, but I am not sure about this situation though, when I want to remove multiple lines in specific location. So quick summary is I want to know if there is a way to remove multiple lines in specific location and how to remove the excessive lines in my text document.

Thanks in advancement, I would appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Removing items from a text file

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Removing items from a text file

Posted 20 March 2013 - 04:48 PM

first off, a text file is not a database.

Yes, you can easily remove whitespace. I like to read a text file into a list, then iterate over that list, something like this...
with open("myFile.txt") as f:
    lines = f.readlines()

output = []

for line in lines:
    #at this point I can start iterating over my list and building an output list
    #If I want to remove excess whitespace, I can simply choose not to put those lines into the output list
    #I can also remove an entire section if that's what I need to do, for example I can check if the line contains a substring, like "john" for example, and then choose not to add those lines to the outputList




Does this help at all?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1