log.info("Getting data...") _FilesAndData = list() try: for name, query, headings in _listActiveQueries: _ShortName, _ = name.split(".") RS = Bcur.execute(query) fh = StringIO() if RS: log.info("'{_ShortName}' data retrieved. Generating CSV.".format( **locals())) strHeadings = ",".join( [repr(i).replace("'", '"') for i in headings]) fh.write(strHeadings + "\n") for row in RS: row = ",".join( tuple([ '"' + str(val).replace('"', "") + '"' if val is not None else '""' for val in row ])) fh.write(row + "\n") if _WRITE_CSV_TO_DISK: log.info("Writing {name} to disk.".format(**locals())) fh.seek(0) with open(name, 'w') as csvFile: csvFile.write(fh.read()) _FilesAndData.append([name, fh.getvalue()]) else: