[英]How do I overwrite a specific line in a CSV file with Python
我正在制作一个程序,让您将数据输入到 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()
您可以将 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]
我希望这可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.