如何让python从csv文件中删除我搜索的行
我可以编辑和添加新条目,但每次我尝试删除我搜索的行时,它都会擦除整个文件数据 我需要它在不丢失其余行的情况下删除该行数据。如何让python从csv文件中删除我搜索的行,python,csv,Python,Csv,我可以编辑和添加新条目,但每次我尝试删除我搜索的行时,它都会擦除整个文件数据 我需要它在不丢失其余行的情况下删除该行数据。 import csv,os,sys def helper(file): o=csv.reader(open(file,"r")) for row in o: print row def delete(filename): found=False f1=csv.reader(open(filename,'r'))
import csv,os,sys
def helper(file):
o=csv.reader(open(file,"r"))
for row in o:
print row
def delete(filename):
found=False
f1=csv.reader(open(filename,'r'))
f2=csv.writer(open("temp.csv",'a'))
rid=raw_input("Enter name to find record:")
for row in f1:
if rid in row[0]:
found=True
f2.writerow()
print rid, "has been deleted from the database!"
else:
found=False
if found==False:
print "That name isn't in our database!"
z=raw_input("Please enter 1 to retry, 2 to return to log in, or 3 to close program:")
if z=="1":
delete(filename)
if z=="2":
import program
if z=="3":
exit
helping=raw_input("Do you require any help with using this feature?Type y for yes or just hit enter to continue:")
if helping=="y":
helper('deletehelp.txt')
delete("custdet.csv")
os.remove("custdet.csv")
os.rename("temp.csv","custdet.csv") #This is the file rename that I mentioned above.
restart=raw_input("Would you like to return to the main menu? Please type Y or just hit enter to exit:")
if restart=="y":
import program
else: exit
您从未实际写入新的csv文件 查看主筛选/复制循环:
# for every row in the input file
for row in f1:
# Is this the/a record to delete?
if rid in row[0]:
# yes!
found=True
# then we do not write anything to the output file (???)
f2.writerow()
print rid, "has been deleted from the database!"
# no else part, so no nothing gets done with
# the input row `row` ...
因此,您将得到一个空的输出文件…您从未真正写入新的csv文件
import csv,os,sys
def helper(file):
o = csv.reader(open(file,"r"))
for row in o:
print row
def delete(filename):
f1 = csv.reader(open(filename,'r'))
f2 = csv.writer(open("temp.csv",'a'))
rid = raw_input("Enter name to find record:")
found = False
for row in f1:
if rid not in row[0]:
f2.writerow(row)
else:
found = True
print rid, "has been deleted from the database!"
if found == False:
print "That name isn't in our database!"
z = raw_input("Please enter 1 to retry, 2 to return to log in, or 3 to close program:")
if z == "1":
delete(filename)
if z == "2":
import program
if z == "3":
exit
查看主筛选/复制循环:
# for every row in the input file
for row in f1:
# Is this the/a record to delete?
if rid in row[0]:
# yes!
found=True
# then we do not write anything to the output file (???)
f2.writerow()
print rid, "has been deleted from the database!"
# no else part, so no nothing gets done with
# the input row `row` ...
因此,您将得到一个空的输出文件…将包含for循环的部分更改为:
import csv,os,sys
def helper(file):
o = csv.reader(open(file,"r"))
for row in o:
print row
def delete(filename):
f1 = csv.reader(open(filename,'r'))
f2 = csv.writer(open("temp.csv",'a'))
rid = raw_input("Enter name to find record:")
found = False
for row in f1:
if rid not in row[0]:
f2.writerow(row)
else:
found = True
print rid, "has been deleted from the database!"
if found == False:
print "That name isn't in our database!"
z = raw_input("Please enter 1 to retry, 2 to return to log in, or 3 to close program:")
if z == "1":
delete(filename)
if z == "2":
import program
if z == "3":
exit
for row in f1:
if rid in row[0]:
found=True
print rid, "has been deleted from the database!"
else:
f2.writerow(row)
这些变化:
- 如果
不在rid
行[0]
- 不要将
标志重置为found
False
- 递归不是处理重试的最佳方法,请改用循环
返回登录?????那是不可能的 工作也许您应该从函数返回导入程序
和调用它的代码之间存在耦合 依赖于正在创建的名为delete()
的文件。那就太多了 如果temp.csv
执行了临时文件本身的重命名,效果会更好delete()
for row in f1:
if rid in row[0]:
found=True
print rid, "has been deleted from the database!"
else:
f2.writerow(row)
这些变化:
- 如果
不在rid
行[0]
- 不要将
标志重置为found
False
- 递归不是处理重试的最佳方法,请改用循环
返回登录?????那是不可能的 工作也许您应该从函数返回导入程序
和调用它的代码之间存在耦合 依赖于正在创建的名为delete()
的文件。那就太多了 如果temp.csv
执行了临时文件本身的重命名,效果会更好delete()