6 Replies - 6631 Views - Last Post: 12 July 2011 - 01:19 PM Rate Topic: -----

#1 yuvinng  Icon User is offline

  • New D.I.C Head

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

problem how to read in .txt using python and delete a line?

Posted 11 July 2011 - 03:19 PM

def delorder():
    f=open("orderlist.txt","r+")
    x=f.readlines()
    n=input("what is the ordernumber to be deleted:")
    s=int(n)
    i=0
    while i<len(x):
        y=x[i]
        z=str(y)
        w=z[1:4]
        d=int(w)
        if d==s:
            for line in temp:
                if not x[d] in line:
            -----------------------------#i have no idea what to write here.. i tried alot ways
            -----------------------------#
            return("succesfully deleted")
        i+=1  


This is my orderlist.txt
[99 ,'1','3/03/2011', [“ScrewDriver”, “Hammer”, “Stone”]]
[2 ,'2', '3/03/2011', ['hammer,studd']]
[31 ,'2', '3/03/2011', ['hammer,studd']]
[100 ,'2', '3/03/2011', ['hammer,nails']]

if my input example (31)
i want to delete the whole line that contain order number(31)---->[31 ,'2', '3/03/2011', ['hammer,studd']]

how can i do that?

please help thank you!

Mod Edit:
:code:

Is This A Good Question/Topic? 0
  • +

Replies To: problem how to read in .txt using python and delete a line?

#2 yuvinng  Icon User is offline

  • New D.I.C Head

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

Re: problem how to read in .txt using python and delete a line?

Posted 11 July 2011 - 03:49 PM

@motama helped me to understand how to make the text in .txt into a list in python. but i still cant dont understand how to delete the whole line properly... the result of the other program deleted everthing in my order list instead of replacing the left over that is not equal to the number. plz help !
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: problem how to read in .txt using python and delete a line?

Posted 11 July 2011 - 03:56 PM

If you want to do it that way, we can do that. However, you NEED to use more descriptive variable names and work with int for order_num
def delorder():
    f=open("orderlist.txt","r+")
    line = f.readlines()
    num_del = int(input("what is the ordernumber to be deleted:"))
    new_string = ""

    for line in lines:
        list_line = eval(line)
        if not (num_del == list_line[0]):
            new_string += (line + "\n")



At the end of this, new_string SHOULD contain every line except the one that you chose to omit.
Was This Post Helpful? 0
  • +
  • -

#4 yuvinng  Icon User is offline

  • New D.I.C Head

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

Re: problem how to read in .txt using python and delete a line?

Posted 11 July 2011 - 07:07 PM

Forget to mention... i need to put it all the not deleted lines back to orderlist.txt and save it ..
i need to use f.append os.remove(file) , os.rename(temp,file) ?

This post has been edited by yuvinng: 11 July 2011 - 07:08 PM

Was This Post Helpful? 0
  • +
  • -

#5 yuvinng  Icon User is offline

  • New D.I.C Head

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

Re: problem how to read in .txt using python and delete a line?

Posted 11 July 2011 - 07:34 PM

Traceback (most recent call last):
File "E:/cscs 120 testint in lab/assignment 1/Customer assignment 1/Customer assignment 1/testing oder", line 13, in <module>
print(delorder())
File "E:/cscs 120 testint in lab/assignment 1/Customer assignment 1/Customer assignment 1/testing oder", line 8, in delorder
listline=eval(line)
File "<string>", line 1
[99 ,'1','3/03/2011', [“ScrewDriver”, “Hammer”, “Stone”]]
^
SyntaxError: invalid character in identifier

i have this error anyway
Was This Post Helpful? 0
  • +
  • -

#6 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: problem how to read in .txt using python and delete a line?

Posted 12 July 2011 - 05:58 AM

dogstoppers program couldn't work. He made an assumption about your orderlist.txt file, that is wrong.
The “ characters are unicode double open/close quotes not ascii double qoutes. So Pythons eval choked on them.
If instead of:
[99 ,'1','3/03/2011', [“ScrewDriver”, “Hammer”, “Stone”]]


it was
[99 ,'1','3/03/2011', ["ScrewDriver", "Hammer", "Stone"]]


dogstoppers code would work.

But you can really use what dogstopper has given you. Read the file, build the contents of the file in memory, rewrite them to the file. Only thing that is left is to extract the ordernumber from a line:
def extract_ordernr(line):
    #for you to write
    #w=line[1:4]
    #d=int(w)
    #wont work as you could have any number of digits
    #and on a line of [1 ,'3' ...
    #w woulb be "1 ,"
    #and int(w) would choke on that
    raise NotImplemented

def delorder():
    f=open("orderlist.txt","r+")
    x=f.readlines()
    n=input("what is the ordernumber to be deleted:")
    s=int(n)
    temp = "" #we build the new contents of f in this string
    for line in x:
        ordernr = extract_ordernr(line)
        if ordernr == s:
            pass
        else:
            temp += line + "\n"
    #we need to close and reopen f now
    #as in "w+" mode f.write appends instead of overwriting
    f.close()
    f = open("orderlist.txt","w")
    f.write(temp)
    #make it clean, close open files, dont wait for the garbage collector to do so
    f.close()
    return ("succesfully deleted")


Was This Post Helpful? 3
  • +
  • -

#7 yuvinng  Icon User is offline

  • New D.I.C Head

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

Re: problem how to read in .txt using python and delete a line?

Posted 12 July 2011 - 01:19 PM

Yeah that was my biggest mistake... i made changes after that and i work fine. thank all of you. The reason is i copy the list from microsoft word. the "" is not the same as in python or notepad. thank you =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1