如何让python删除我从csv文件中搜索的行

时间:2016-01-23 11:36:38

标签: 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'))
    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

3 个答案:

答案 0 :(得分:1)

你实际上从未写过新的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` ...

所以你最终得到一个空的输出文件...

答案 1 :(得分:1)

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

答案 2 :(得分:0)

将包含for循环的部分更改为:

    for row in f1:
        if rid in row[0]:
            found=True
            print rid, "has been deleted from the database!"
        else:
            f2.writerow(row)

变化:

  • 如果rid
  • 中不存在row[0],请执行该行
  • 不要将found标记重置为False

其余代码也需要做一些工作:

  • 递归不是处理重试的最佳方法,而是使用循环。
  • import program返回登录?????那不行 工作。也许你应该从函数返回。
  • delete()与调用代码之间存在耦合 依赖于正在创建的名为temp.csv的文件。这会很多 如果delete()执行了临时文件本身的重命名,则会更好。