3 Replies - 16127 Views - Last Post: 11 July 2011 - 05:48 AM Rate Topic: -----

#1 yuvinng   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-July 11

question how can i delete words in .txt using python

Posted 10 July 2011 - 07:35 PM

my list are
[1, 'Yuvin Ng', 'Columbia College', 778]
[2, 'Ali', 'Douiglas College', 77238]
[3, 'Nancy', 'Douglas College', 7783390222]

my codes:
def delcustomer():
    import os
    f=open("customerlist.txt","r+")
    entername=input("enter customer name")
    for line in f:
        if entername in line:
            f.delete(line)# i know is shoudn;t suppose to be like this but im seriously stuck



Is there anyway to delete a list in .txt using python?
for example if name input is Yuvin Ng and i want to delete the whole line if it match. How can i do that?

This post has been edited by baavgai: 11 July 2011 - 04:30 AM
Reason for edit:: tagged


Is This A Good Question/Topic? 0
  • +

Replies To: question how can i delete words in .txt using python

#2 Eric115   User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 698
  • Joined: 19-January 09

Re: question how can i delete words in .txt using python

Posted 11 July 2011 - 02:27 AM

From the looks of your code, it looks like you might be doing something which would be easier if you used a database. If your program is designed to store, modify and delete names/info like you have above, I would recommend looking into sqlite3 (There is a tutorial in the Python section on DIC). If you still want to do it using a text file, you could read the file and then sort through each entry and add all the ones you want to a new file and the one that you don't, just skip over. I made this script to demonstrate:
f = open('test.txt', 'r')
read = f.readlines()
f.close()
print 'r', read
name = 'Yuvin Ng' # set this to what ever you are looking for
newfile = """"""
for i in read:
    print i
    print i[1]
    if name in i:
        pass
    else:
        newfile += i
n = open('test.txt', 'w')
one = str(newfile)
n.write(one)
n.close()


Good luck
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7205
  • View blog
  • Posts: 15,018
  • Joined: 16-October 07

Re: question how can i delete words in .txt using python

Posted 11 July 2011 - 04:44 AM

You have a list and you have a file; how do they relate?

The list is stored in the file? What format?

On approach would be to have a file that you save to and load from as you manipulate the list...

def saveData(data, filename):
	# your code here

def loadData(filename):
	# your code here

def deleteName(data, name):
	# your code here

clist = loadData("customerlist.txt")
deleteName(clist, 'Yuvin Ng')
saveData(clist,"customerlist.txt")


Was This Post Helpful? 0
  • +
  • -

#4 Motoma   User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 798
  • Joined: 08-June 10

Re: question how can i delete words in .txt using python

Posted 11 July 2011 - 05:48 AM

The easiest way to do this would be to read the file into a list, and then write the file back without the line you want to delete:

def delcustomer():
    import os

    tmp = []
    f = open("customerlist.txt","r")
    for line in f:
        tmp.append(line)
    f.close()
    f = open(""customerlist.txt", "w")

    entername = input("enter customer name")
    for line in tmp:
        if not entername in line:
            f.write(line)
    f.close()


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1