delete a line in .txt using python by matching the number in the line -



delete a line in .txt using python by matching the number in the line -

i came lastly part.. , i'm stuck..

in orderlist.txt file have

[99 ,1, "3/03/2011", ["screwdriver", "hammer", "stone"]] [2 ,2, "3/03/2011", ["hammer,nails"]] [31 ,2, "3/03/2011", ["plaster,studd"]] [100 ,2, "3/03/2011", ["hammer,studd"]]

my code:

def delorder(): f=open('orderlist.txt',"r+") lines=f.readlines() num_del=int(input("what ordernumber deleted:")) new="" line in lines: listline=eval(line) if not(num_del==list_line[0]): new +=(line + "\n") homecoming (new)

at part give me

traceback (most recent phone 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

my objective of function input number example:31 , delete--> [31 ,2, "3/03/2011", ["plaster,studd"]], @ end in orderlist.txt not appearr line [31 ,2, "3/03/2011", ["plaster,studd"]] left on lines. plz help

i'm not sure if see them, closely @ quotation marks:

[1,'1','3/03/2011', [“screwdriver”, “hammer”, “stone”]]

there's difference between this:

and this:

"

the first 1 () curly quote, screws interpreter. utilize normal quotes (") , post script returns then.

also, return statement do:

return (new)

if you're in function, function quit upon first iteration of list, returns every single loop, first 1 caught , function dies there.

i'd move return statement downwards level isn't part of for loop. alter this:

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

to this:

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

this text file without curly quotes. seek re-create , pasting it:

[99 ,1, "3/03/2011", ["screwdriver", "hammer", "stone"]] [2 ,2, "3/03/2011", ["hammer,nails"]] [31 ,2, "3/03/2011", ["plaster,studd"]] [100 ,2, "3/03/2011", ["hammer,studd"]]

python

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -