【问题标题】:Removing a line from a file从文件中删除一行
【发布时间】:2021-07-12 13:54:44
【问题描述】:

我有一个名为 (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

最后一个逗号后的数字是已售商品。我想写一个代码,只要最后一个逗号后面的数字是0,就可以删除一行。这是我写的代码,但即使最后一个逗号后面的数字不为零,它也会删除该行:

 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")

【问题讨论】:

  • 产品ID是开头的6位数字,我在代码中定义了。
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)应包括所需的行为、特定问题或错误所需的最短代码作为格式化文本(不是图像)在问题本身中。没有明确的问题陈述的问题对其他读者没有用处。请参阅:minimal reproducible example
  • 我的问题是即使最后一个逗号后面的数字不为零,这段代码也会删除该行
  • y 是在哪里定义的? productID 呢?请发帖minimal reproducible example
  • y 的值从未使用过,应从您的代码中删除。它不是最小示例的一部分

标签: python-3.x file


【解决方案1】:

很遗憾我不明白您的要求。如果您难以表达一个难题,请尝试向朋友提问,然后写下您说的话。

除了y 无缘无故引入的噪音之外,这段代码的另一个奇怪之处在于这种比较:

productInfo[5]>"0"

可能这种比较并没有达到您的预期。

我相信您只想知道最后一个标记是否为“0”。为此,最好测试是否相等,而不是尝试对两个字符串进行值比较。

可以使用== 测试字符串相等性。使用!= 检查不等式。

我相信你想要这个:

productInfo[5] != "0"

【讨论】:

    【解决方案2】:

    据我了解,您有一个包含逗号分隔数据的文件,最后一个值是您感兴趣的。如果该值为 0,则您要删除该行。

    一般的想法是读取文件中的行并将,拆分为行并访问最后一项。正如许多人指出的那样,您的错误是,您试图将字符串与> 进行比较,这在python 中无效。以下代码适用于您的示例数据:

    #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
    

    【讨论】:

    • 是的,这就是我的意思,但我不想删除所有包含 0 的行,我想删除一个。例如,当输入是 (463254),即文件第三行中的 line[0] 时,我只想删除这一行。
    • 绝对支持在 Python 中比较字符串:"0" < "1" is True
    猜你喜欢
    • 2013-05-13
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 2020-12-17
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多