如何使用Python覆盖CSV文件中的特定行

时间:2018-10-24 08:14:52

标签: python python-3.x csv

我正在制作一个程序,该程序可让您将数据输入CSV文件,然后让您搜索特定客户,并从系统中删除特定客户。我已经完成了查看部分,但无法弄清楚如何覆盖csv文件中的特定行。这是我到目前为止的内容:

    choice = str(input("What would you like to do?"))
    if choice.lower() == "delete":
        Type = str(input("Type in 'Phone' to delete by Phone number and 'Name' to delete by Surnames: "))
        if Type.lower() == "phone":
            view = str(input("Enter the phone number of the customer you want to delete: "))
            check = csv.reader(open('D:\Programming\School Work\Johns Decorating company program\Customers.csv', "rt"), delimiter=",")
            for line in check:
                if view in line:
                    print(line)
                    confirm = str(input("Type 'Yes' to delete all of the quotes above. Type 'No' to restart or to select in a different way"))
                    if confirm.lower() == "yes":
                        #!THIS IS WHERE I'M CONFUSED!
                    elif confirm.lower() == "no":
                        print("Restarting search")
                        existing()

1 个答案:

答案 0 :(得分:0)

您可以将csv转换为列表并编写:

for i in range(len(check)):
  if check[i][column_index] == phone_number:  # column_index is index
                                              # of a column where the phone numbers are stored
   del csv_list[i]

希望对您有帮助。