我编写的代码是保存错误元素并另存为ledger_name_error.txt
,我还必须创建一个不包含错误元素的新文本文件。
在Corrected.txt
上,它仅应包含(Americano
,Cafe_latte
,Cafe_mocha
,Berry_smoothie
,Plain_yogurt
)
有一些随机的饮料名称,我必须对它们进行排序并将正确的饮料名称保存到corrected.txt
中。
def error_check(date):
#========= STEP 3 ==========
Record = []
errormenu = []
recordfile = open("ledger_"+date+".txt","r")
errormenufile = open("menu.txt", "r")
for error in errormenufile:
menu = error.split()
errormenu.append(menu[0])
errormenufile.close()
for line in recordfile:
record = line.split()
Record.append(record)
Record = [x for x in Record if errormenu[0] not in x]
Record = [x for x in Record if errormenu[1] not in x]
Record = [x for x in Record if errormenu[2] not in x]
Record = [x for x in Record if errormenu[3] not in x]
Record = [x for x in Record if errormenu[4] not in x]
name_error_record = open("ledger_"+date+"_name_error.txt","w")
for record in Record:
name_error_record.write( record[0]+" "+record[1]+" "+record[2]+" "+record[3]+"\n")
name_error_record.close()
#========= STEP 3 ==========