如何将代码拆分为更小的函数

时间:2015-03-17 16:38:29

标签: python function class csv

我有一个有效的应用程序。但是为了试图更好地理解函数和python。我试图把它分成各种函数。

我坚持使用file_IO函数。我确定它不起作用的原因是因为应用程序的主要部分不理解读者或编写者。为了更好地解释。这是应用程序的完整副本。

我也很好奇使用csv.DictReader和csv.DictWriter。是否为当前代码提供任何优点/缺点?

我想另一种方法是通过课程,老实说,我也想知道如何以这种方式去做。

#!/usr/bin/python

""" Description This script will take a csv file and parse it looking for specific criteria.  
A new file is then created based of the original file name containing only the desired parsed criteria.
"""

import csv
import re
import sys

searched = ['aircheck', 'linkrunner at', 'onetouch at']

def find_group(row):
    """Return the group index of a row
        0 if the row contains searched[0]
        1 if the row contains searched[1]
        etc
        -1 if not found
    """
    for col in row:
        col = col.lower()
        for j, s in enumerate(searched):
            if s in col:
                return j
        return -1



#Prompt for File Name
def file_IO():
    print "Please Enter a File Name, (Without .csv extension): ",
    base_Name = raw_input()
    print "You entered: ",base_Name

    in_Name = base_Name + ".csv"
    out_Name = base_Name + ".parsed.csv"

    print "Input File: ", in_Name
    print "OutPut Files: ", out_Name

    #Opens Input file for read and output file to write.
    in_File = open(in_Name, "rU")
    reader = csv.reader(in_File)

    out_File = open(out_Name, "wb")
    writer = csv.writer(out_File, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)

    return (reader, writer)

file_IO()

# Read header
header = reader.next()


stored = []
writer.writerow([header[0], header[3]])

for i, row in enumerate(reader):
    g = find_group(row)
    if g >= 0:
        stored.append((g, i, row))
stored.sort()

for g, i, row in stored:
    writer.writerow([row[0], row[3]])


# Closing Input and Output files.
in_File.close()
out_File.close()

1 个答案:

答案 0 :(得分:0)

如果我是你,我只会将find_group分开。

import csv

def find_group(row):
    GROUPS = ['aircheck', 'linkrunner at', 'onetouch at']
    for idx, group in enumerate(GROUPS):
        if group in map(str.lower, row):
            return idx
    return -1

def get_filenames():
    # this might be the only other thing you'd want to factor
    # into a function, and frankly I don't really like getting
    # user input this way anyway....
    basename = raw_input("Enter a base filename (no extension): ")
    infilename = basename + ".csv"
    outfilename = basename + ".parsed.csv"
    return infilename, outfilename
    # notice that I don't open the files yet -- let main handle that

infilename, outfilename = get_filenames()

with open(infilename, 'rU') as inf, open(outfilename, 'wb') as outf:
    reader = csv.reader(inf)
    writer = csv.writer(outf, delimiter=',',
                        quotechar='"', quoting=csv.QUOTE_ALL)
    header = next(reader)
    writer.writerow([[header[0], header[3]])
    stored = sorted([(find_group(row),idx,row) for idx,row in
                     enumerate(reader)) if find_group(row) >= 0])
    for _, _, row in stored:
        writer.writerow([row[0], row[3]])