使用python从文件中删除记录
我正在用python 3.6创建一个非常基本的银行记录程序。我创建了一个文件“accounts.txt”,用户可以在其中添加他们的帐户——姓名、帐号、余额。添加后,文件的外观如下所示:使用python从文件中删除记录,python,Python,我正在用python 3.6创建一个非常基本的银行记录程序。我创建了一个文件“accounts.txt”,用户可以在其中添加他们的帐户——姓名、帐号、余额。添加后,文件的外观如下所示: f = open("filename.txt").readlines() f = [i.strip('\n') for i in f] file = [map(int, i.split()) for i in f] #then, you can loop through file and see if t
f = open("filename.txt").readlines()
f = [i.strip('\n') for i in f]
file = [map(int, i.split()) for i in f]
#then, you can loop through file and see if the account number exists:
#account numbers are first in the list, so you can make a list of them
account_numbers = [i[0] for i in file]
number = 123456
if number in account_numbers:
print "account number ", number, " all ready exists"
else:
print "account number", number, " does not exist"
213123 | dgfdg | 21312.0123124 | vxcvx | 213123.0
123123 | sfsdf | 123123.0 列分别表示账号、名称和余额 我想从此文件中删除一条特定记录。我将账号作为要删除记录的输入。我就是不知道怎么做。如果用户输入213123,则应删除整个记录 在同样的情况下还有一个单独的问题——当我要求用户输入他们的账号、姓名、余额时,我想扫描“accounts.txt”,如果账号已经存在,则生成一个错误,而不是创建一个新记录。避免重复帐号 我是python新手,这是一个基本程序,所以请不要建议使用mysql或mariadb:D
def account_set():
accno = int(input("Enter Account Number \t"))
name = str(input("Enter Name \t"))
balance = float(input("Enter Balance \t"))
return accno, name, balance
def new():
account = open("accounts.txt", "a+")
info = account_set()
account.seek(0, 2)
for items in info:
account.write("{0:<25} {1}".format(items, "|"))
account.write("\n")
account.close()
def delete():
accno = (input("Enter Account number to delete"))
file = open("accounts.txt", "r+")
print("""
Please choose any one of the Following Options: \n \n
1. New Account \n
2. Delete Account \n
3. Exit
""")
x = True
while x:
user_input = int(input("Enter an option \t"))
if user_input == 1:
new()
elif user_input == 2:
delete()
elif user_input == 3:
print("Thank you for banking with us.")
x = False
else:
print("Invalid Choice")
def account_set():
accno=int(输入(“输入帐号”)
name=str(输入(“输入名称\t”))
平衡=浮动(输入(“输入平衡\t”))
返回账号、名称、余额
def new():
账户=未结(“accounts.txt”、“a+”)
info=科目集合()
帐户搜索(0,2)
对于信息中的项目:
account.write(“{0:一种可能的方法是要求用户输入并查看它是什么数字。如果数字是213123,则可以使用以下代码:
filename.seek(0)
filename.truncate()
上面的代码将删除整个文件
这篇文章解释了如何完美地删除单行:
要查看是否存在帐号,您可以按如下方式读取文件:
f = open("filename.txt").readlines()
f = [i.strip('\n') for i in f]
file = [map(int, i.split()) for i in f]
#then, you can loop through file and see if the account number exists:
#account numbers are first in the list, so you can make a list of them
account_numbers = [i[0] for i in file]
number = 123456
if number in account_numbers:
print "account number ", number, " all ready exists"
else:
print "account number", number, " does not exist"
欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南。并在此处申请。StackOverflow不是设计、编码、研究或教程服务。“我只是不知道如何做。”“建议您需要一个小时与本地导师一起学习如何分析问题陈述;堆栈溢出用于特定的编程问题。我只能使用此选项:filename.seek(0)filename.truncate()如果我知道记录在哪个位置。我需要一个通用的算法,因为每次我想删除记录时,我不会打开文件查看记录在哪里,然后更改代码删除特定的记录。希望你能理解。你提供的链接也不能解决我的问题。因为假设某个记录在nt编号123和其他一些记录的余额为123。如果我简单地检查每行的“123”,这两个记录都将被删除。