python 2.7 - tempfile object skips lines in for loop -
i read data csv file, perform normalization on each row , write temp file. temp file object created in main() function code in 1 file writes , code in file reads it. avoid importing large files memory.
however, following script writes subset of rows file. when replace temp_file.write(row)
print
statement rows printed stdout.
can me understand why print
returns rows subset written temp file.
update: added normalize()
method main()
method , results correct. suspect issue passing tempfile object name python file , writing file.
main method
def main(): [argparse code here - removed brevity] temp_file = namedtemporaryfile() if os.path.isfile(args.input_file): normalize.normalize_file(args.input_file, temp_file) else: print 'please supply valid source file' if __name__ == '__main__': main()
method imported file
def normalize_file(csv_file, temp_file): open(csv_file) file: reader = csv.reader(file) line in file: row = line.lower() matchstr = re.search(r'\\users\\\w+\\',row, re.i) if matchstr: row = row.replace(matchstr.group(), '\\users\\usr\\') matchstr = re.search(r'\w:\\',row, re.i) if matchstr: row = row.replace(matchstr.group(), '\\') temp_file.write(row) #if put print here rows printed out correctly
Comments
Post a Comment