简体   繁体   English

如何从用户指定的 txt 列表中删除行?

[英]How can I remove line from list in txt as specified by user?

Python 101 here. Python 101 在这里。 I've created a program for a coffee shop owner to keep track of inventory.我为咖啡店老板创建了一个程序来跟踪库存。 Im supposed to "Modify the file by allowing the owner to remove data from the file. Ask the owner to enter a description to remove. If the description exists, remove the coffee name and the quantity. If the description is not found, display the message: That item was not found in the file."我应该“通过允许所有者从文件中删除数据来修改文件。要求所有者输入要删除的描述。如果描述存在,请删除咖啡名称和数量。如果找不到描述,则显示消息:在文件中找不到该项目。” So far the advice I have received is to read the file content to a list of lines, filter out the lines I don't want and write the list back to the file.到目前为止,我收到的建议是将文件内容读取到行列表中,过滤掉我不想要的行并将列表写回文件。

Here's what I have so far这是我到目前为止所拥有的

with open('coffeeInventory.txt', 'w') as f:
    f.write('Blonde Roast=15\n')
    f.write('Medium Roast=21\n')
    f.write('Flavored Roast=10\n')
    f.write('Dark Roast=12\n')
    f.write('Costa Rica Tarrazu=18\n')
    f.close()
sum=0

with open('coffeeInventory.txt', 'r') as f:
    for line in f.readlines():
        sum += int(line.split("=")[1])
        print(line)
f.close()

print('Total Pounds of Coffee= ', sum)

with open('coffeeInventory.txt') as f:
  lineList = f.readlines()
lineList= [line.rstrip('\n') for line in open('coffeeInventory.txt')]
print(lineList)

Perhaps this can get you started也许这可以让你开始

import io

f = io.StringIO()

f.write('Blonde Roast=15\n')
f.write('Medium Roast=21\n')
f.write('Flavored Roast=10\n')
f.write('Dark Roast=12\n')
f.write('Costa Rica Tarrazu=18\n')

f.seek(0)

lineList = f.readlines()

print(lineList)

deleteThis = 'Dark Roast'

newList = [line for line in lineList if deleteThis not in line]

print(newList)

I would go another way from the other answers and actually suggest a JSON file.我会 go 从其他答案的另一种方式,实际上建议 JSON 文件。

Firstly, you have to create a dictionary.首先,您必须创建一个字典。 Each element in a dictionary has a key and a value.字典中的每个元素都有一个键和一个值。

coffee_inventory = {
    "Blonde Roast": 15
}

You can access the inventory by specifying the name within square brackets.您可以通过在方括号中指定名称来访问库存。

coffee_inventory["Blonde Roast"]

Output: 15

To add a key and a value to a dictionary all you have to do is specify the key in the square brackets and then an equals sign and the value.要将键和值添加到字典中,您只需在方括号中指定键,然后指定等号和值。

coffee_inventory["Medium Roast"] = 21

Thus, now the dictionary would be as such:因此,现在字典将是这样的:

coffee_inventory = {
    "Blonde Roast": 15,
    "Medium Roast": 21
}

You will have to use JSON to save and retrieve the file.您必须使用 JSON 来保存和检索文件。

import json

coffee_inventory = {
    "Blonde Roast": 15,
    "Medium Roast": 21
}

# Saving the JSON to a file.
# "indent=4" is for better styling.
with open("inventory.json", "w") as file:
    json.dump(coffee_inventory, file, indent=4)

# Retrieving the JSON.
with open("inventory.json", "r") as file:
    coffee_inventory = json.load(file)

print(coffee_inventory)

Output: {'Blonde Roast': 15, 'Medium Roast': 21}

Lastly to delete a value you just use del .最后要删除一个值,您只需使用del For example to delete "Blonde Roast" you do the following.例如,要删除“Blonde Roast”,请执行以下操作。

del coffee_inventory["Blonde Roast"]

This is all you need to know about JSON and I am sure you can connect everything from this guide to your program and make this even better than before.这就是您需要了解的有关 JSON 的所有信息,我相信您可以将本指南中的所有内容连接到您的程序,并使其比以前更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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