def remove_chars(value, chars): """ remove characters from the string value (also removes leading and trailing spaces""" if isinstance(value, str): value=value.lstrip() value=value.rstrip() for __char in chars: value=value.replace(__char, '') return value if __name__ == '__main__': import os, re, csv, time estimate_file=csv.writer(file(r"p:\mcdc\acs2007_3yr\profiles_estimates3yr.csv", "wb")) moe_file=csv.writer(file(r"p:\mcdc\acs2007_3yr\profiles_moe3yr.csv", "wb")) for root, dirs, files in os.walk(r"p:\mcdc\acs2007_3yr\profiles\\"): for __file in files: if __file.endswith(".csv") and __file.startswith("Profile"): print __file, time.ctime() __cur_file=csv.reader(file(os.path.join(root, __file), "rb"), dialect='excel') #open file for reading __cur_file.next() __estimate_data=[] __moe_data=[] for __row in __cur_file: #read each line of data if __estimate_data == []: __estimate_data.append(__row[0]) #append geoid if __moe_data==[]: __moe_data.append(__row[0]) #append geoid if len(__row) >= 4: __col=remove_chars(__row[3], ' ') if __col != '0' and __col.find('.') == -1: __estimate=remove_chars(__row[5], ',+/-$') __moe=remove_chars(__row[6], ',+/-$') if __moe.startswith('**') and __moe.endswith('**'): __moe='0' __estimate_data.append(__estimate) #add data to list of items to add to csv files __moe_data.append(__moe) del __cur_file estimate_file.writerow(__estimate_data) moe_file.writerow(__moe_data) else: print "Something Wrong with %s" % __file del estimate_file del moe_file