如何使用 Python 覆盖 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:ProgrammingSchool WorkJohns Decorating company programCustomers.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()

您可以将 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]

我希望这对你有所帮助。

最新更新