使用python在csv中第一次出现空行后查找并删除

时间:2013-10-01 21:14:45

标签: python csv

我想在第一次出现空行后删除所有行。

  1. 在遇到空行之前读取行的最佳方法是什么?
  2. 删除所有正在进行的行的最佳命令是什么?

1 个答案:

答案 0 :(得分:0)

这是您可以使用的脚本。 main函数中的代码只是为了允许您选择一个文件(或者如果给定的话,将其选为命令行参数)。然后调用crop_file函数,通过打开文件,将数据读取到第一个空行到内存中,然后关闭文件然后将存储的数据写入同一文件(如果您确认要其余的行已删除。

如果您对代码有任何疑问,请与我们联系。

#!/usr/bin/python

import os, csv, sys, Tkinter, tkFileDialog as fd

# stop tinker shell from opening as only needed for file dialog
root = Tkinter.Tk()
root.withdraw()

def crop_file(in_path):
    # read file into memory
    file_obj = open(in_path, 'rb')
    reader = csv.reader(file_obj, delimiter='\t')
    data = []
    for row in reader:
        if not row or not any(row):
            break #stop at empty row
        else:
            data.append(row)
    file_obj.close()

    print 'Found', len(data), 'rows of data without empty lines.'
    conf = raw_input('delete remaining lines? (Y|N): ').upper()[0]

    if conf == 'Y':
        # write data to file
        file_obj = open(in_path, 'wb')
        writer = csv.writer(file_obj)
        writer.writerows(data)
        file_obj.close

def main():
    in_path = None
    prog_name = sys.argv[0]

    # check if in_path are inlcuded as cmd line args...
    if len(sys.argv) > 1:
        in_path = sys.argv[1]
        if not os.path.exists(in_path):
            print 'Usage:', prog_name, '[file_path>]'
            print 'cannot find the file provided for file_path:\n', in_path
            sys.exit("Error - invalid excel_file_path arg")
    else:
        try:
            # set current working directory to user's my documents folder
            os.chdir(os.path.join(os.getenv('userprofile'),'documents'))
        except:
            pass

    # ask user for path to file...
    while not in_path:
        print "Please select the file to read data from ..."
        try:
            in_path = fd.askopenfilename()
        except:
            print 'Error selecting file.'
        if not in_path:
            cont = raw_input('Do you want to continue? (Y|N): ').upper()[0]
            if cont == 'N':
                sys.exit("Error - unable to select input file")

    crop_file(in_path)

if __name__ == '__main__':
    main()