I finally came to my last part.. and I'm stuck..
In my orderlist.txt file I 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 is the ordernumber to be deleted:"))
new=""
for line in lines:
listline=eval(line)
if not(num_del==list_line[0]):
new +=(line + "\n")
return (new)
at this part it give me
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
My objective of this function is to get an input number example:31 and it will delete--> [31 ,2, "3/03/2011", ["plaster,studd"]], which at the end in the orderlist.txt it would not appearr this line [31 ,2, "3/03/2011", ["plaster,studd"]] but the left over lines. plz help
I'm not sure if you see them, but look closely at those quotation marks:
[1,'1','3/03/2011', [“ScrewDriver”, “Hammer”, “Stone”]]
There's a difference between this:
”
and this:
"
The first one (”
) is a curly quote, which screws up the interpreter. Use normal quotes ("
) and post back what your script returns then.
Also, what does this return
statement do:
return (new)
If you're in a function, the function will quit upon the first iteration of the list, as it return
s every single loop, but only the first one is caught and the function dies there.
I'd move the return
statement down a level so it isn't part of the for
loop. So change this:
for line in lines:
listline=eval(line)
if not(num_del==list_line[0]):
new +=(line + "\n")
return (new)
To this:
for line in lines:
listline=eval(line)
if not(num_del==list_line[0]):
new +=(line + "\n")
return (new)
This is your text file without the curly quotes. Try copy and 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"]]
精彩评论