简体   繁体   中英

Removing a line from a file

I have a file named (data.txt):

243521,Biscuit,Flour:Cream,89.5,9,1
367534,Bread,Flour,67.3,1,2
463254,Chocolate,Cocoa butter:Sugar:Milk powder,45.6,4,0
120014,Buns,Wheat Flour,24.9,5,2
560214,Cake,Flour:Baking Powder:Cake Mix,70.5,3,1
123456,burger,bread crumbs:beef:tomato,99.9,10,0

The numbers after the last comma is sold items. I want to write a code that can delete a line just if the number after the last comma is 0. This is the code I wrote but it removes the line even if the number after the last comma is not zero:

 productID=input("")
 with open("data.txt","r+") as file:
        lines= file.readlines()
        file.seek(0)
        for line in lines:
            productInfo= line.split(",")
            y=0
            if productInfo[5]>"0":
                if y==0:
                    print("Product cannot be removed: sold items must be 0")
                    y=1
            elif productID not in line:             
                file.write(line)
                file.truncate()
                print("Product is removed successfully")

I regret that I do not understand what you are asking for. If you have trouble expressing a difficult question, try asking the question to a friend, and then write down what you say.

Other than the noise that y introduces for no reason, the other odd thing about this code is this comparison:

productInfo[5]>"0"

Probably that comparison does not do what you expect.

I believe you just want to know if the last token is a "0" or not. For this it is better to test for equality or inequality, instead of attempting to perform a value comparison of two strings.

String equality can be tested with == . Check for inequality with != .

I believe you want this:

productInfo[5] != "0"

From what I have understood, you have a file that contains comma-separated data and last value is of your interest. If that value is 0, you want to remove that line.

General idea is to read the lines in file and split the , in line and access last item. Your mistake is, as many have pointed out, you are trying to compare strings with > which is not valid in python. Following code works for me with your sample data:

#reading the lines in data as list
with open("data.txt", "r") as f:
    lines = f.readlines()

new_array = []
#empty array so we can populate it with lines that don't have a 0 at the end

user_input = input("Enter a number: ") #Capturing user input

for line in lines: #iterating over all lines
    line = line.split(",") #splitting , in line
    if line[len(line)-1].strip() != "0" and line[0] != user_input:
        #if first item and last item are not user input and 0 respectively
        new_array.append(",".join(line))
    elif line[len(line)-1].strip() == "0" and line[0] != user_input:
        #if last item is 0 but first item is not user input
        new_array.append(",".join(line))
    else:
        print("ignoring:", *line)

with open("data2.txt", "w") as f: #creating new data file without 0
    f.writelines(new_array) #writing new array to new datafile

#data2.txt now contains only lines that have no 0 at the end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM