Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让python从csv文件中删除我搜索的行_Python_Csv - Fatal编程技术网

如何让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循环的部分更改为:

    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()
    执行了临时文件本身的重命名,效果会更好

也许你的f2.writerow()应该在if语句之外!!!是的,它也是空的。你能告诉我你的意思吗?如果你想删除用户输入的行,你肯定不想将它写入你的新文件!所以它应该在if块之外。也许你的f2.writerow()应该在if语句之外!!!是的,它也是空的。你能告诉我你的意思吗?如果你想删除用户输入的行,你肯定不想将它写入你的新文件!所以它应该在if块之外。