Thursday 15 January 2015

python - How to split code into smaller functions -


I have an application that works but in the interest of an effort to better understand the function and python. I am trying to divide it into various functions. I am stuck at the file_IO function. I am sure that the reason is not working because the main part of the application does not understand the reader or there is a complete copy of the application to better understand it.

In addition, I'm curious about using csv.DictReader and csv.DictWriter. Do any of the benefits / losses in existing code provide?

I think that Another way to do this is through classes. Which honestly I know how to do that, I want to know it. "" "Description This script will CSV file and will parse it in search of specific criteria. Then a new file is created based on the original file name which contains only the desired parse criteria. "Import" CSV import re-imported search = ['aircake', 'linker at', 'onch at'] Df search-group (line): return to group index of line "0" if the row was searched If [0] 1 is searched for in line [1] etc. -1 if not to call in "line": col = col.lower () j, in enumerate (search): s if in col: Return j return - # # For file name def file_IO () #Prompt: Print "Please enter a filename, (without .csv extension):", base_Name = raw_input () print "you Accessed: ", base_Name in_Name = base_Name +" .csv "out_Name = Base_Name +" .parsed.csv "print" Input file: "Output files in print:", Out_net # To read the output file, read and output For file. In_file = open (in_Name, "RU") reader = csv.reader (in_File) out_File = open (out_Name, "wb") author = csv.writer (out_File, delimiter = ',', quotechar = '' ', quoting = Csv.QUOTE_A LL.L) Returns (Reader, Author) file_IO () # Header Header = Reader. NEX (archived) = [] Author. For the author ([Header [0], header [3]]), enumerate the line (reader): G = find_group (line) if G & gt; = 0: archived .append (stored in the file), stored.sort () for g), in the row I stored: writer.writerow ([row [0], line [3]]) #put input and output If I were you files, then I was 'I', but I did not know, D only different find_group . (GROUPS): Group in the group (['aircheck', 'linkrunner at', 'onetouch at'] for GROUPS = [idx]

In the map (str.lower, line): return idx return -1 def get_filenames (): # This may be the only other thing you want to do # difference in the function, and obviously I prefer to implement the # user anyway Is not .... basename = raw_input ("Enter the base file name (no extension):") infilename = basename + ".csv" outfilename = basename + ". Parsed.csv" return infilinem, outfile name # is notice That i am still Open the file - open primarily unfilename, openfilename = get_filenames () with open (infilineum, 'ru') as inf, Open (Output name, 'wb') Outf: Reader = csv.reader (inf) Author = csv.writer (outf, deli miter = ',', quotation = '', quoting = csv.QUOTE_ALL) header = next (reader) author. ([[Header [0], header [3]] stored = Sorted (line ([[find_group (line ([[0], line [3], for line [3]), _, _, in row: for adx, idx, line) in enumerate (reader) Address_group (line) & gt; = 0]])

No comments:

Post a Comment