簡體   English   中英

如何將第一個操作的輸出發送到第二個操作的輸入?

[英]How do I send the output of my first operation into the input of the second?

我試圖找到一種方法,可以將在crop_rows調用的crop_rows的輸出發送到delete_Apps直接調用的delete_Apps的輸入中,但是我不確定要喂什么。 另外,我是否需要刪除:

file_obj.close()

來自crop_rows函數,以便我的腳本繼續通過這兩個函數運行?

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


# stop tinker shell from opening as only needed for file dialog

root = Tkinter.Tk()
root.withdraw()


#crop_rows deletes the extra info automatically generated in the Waders report 

def crop_rows(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



#delete_Apps deletes and leads that are currently Applicants in gHire as their Status

def delete_Apps(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 'Applicant' not in row:
            data.append(row)
    file_obj.close()

    print 'Found', len(data), 'Leads with Applicant in gHire as Status.'
    conf = raw_input('delete these leads? (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_rows(in_path)


    delete_Apps(in_path)




if __name__ == '__main__':
    main()

將crop_rows更改為生成器函數(請參閱https://wiki.python.org/moin/Generators ),然后在delete_Apps中使用它。

  1. 分別閱讀.csv文件
  2. 更改crop_rows ,使其將行作為輸入並返回新數據
  3. 然后更改delete_Apps以接受行輸入並返回修改后的數據
  4. 最后編寫.csv文件

然后,您可以簡單地嵌套對函數的調用,如下所示:

def read_input_file(in_path):
    # use with not to worry about closing the file
    with open(in_path, 'rb') as file_obj:
        reader = csv.reader(file_obj, delimiter='\t')
        return list(reader)

def crop_rows(input_rows):
    data = []
    for row in input_rows:
        if not row or not any(row):
            break #stop at empty row
        else:
            data.append(row)

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

    if conf == 'Y':
        return data       
    else:
        # return unmodified data
        return input_rows

def delete_Apps(input_rows):
    data = []
    for row in input_rows:
        if 'Applicant' not in row:
            data.append(row)

    print 'Found', len(data), 'Leads with Applicant in gHire as Status.'
    conf = raw_input('delete these leads? (Y|N): ').upper()[0]

    if conf == 'Y':
        return data
    else:
        return input_rows

def write_output_file(data, out_path):
    wirg open(out_path, 'wb') as file_obj:
    writer = csv.writer(file_obj)
    writer.writerows(data)
    file_obj.close()

最后的電話:

file_path='/tmp/whatever.csv'
write_output_file(delete_Apps(crop_rows(read_input_file(file_path)), file_path)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM