删除列表中未包含的其他元素

时间:2019-05-29 08:39:51

标签: python

我编写的代码是保存错误元素并另存为ledger_name_error.txt,我还必须创建一个不包含错误元素的新文本​​文件。

Corrected.txt上,它仅应包含(AmericanoCafe_latteCafe_mochaBerry_smoothiePlain_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 ==========

0 个答案:

没有答案