def _list_of_dictionaries_to_csv(
            self,
            csvType="human"):
        """Convert a python list of dictionaries to pretty csv output

        **Key Arguments:**
            - ``csvType`` -- human, machine or reST

        **Return:**
            - ``output`` -- the contents of a CSV file
        """
        self.log.debug(
            'starting the ``_list_of_dictionaries_to_csv`` function')

        if not len(self.listOfDictionaries):
            return "NO MATCH"

        dataCopy = copy.deepcopy(self.listOfDictionaries)

        tableColumnNames = dataCopy[0].keys()
        columnWidths = []
        columnWidths[:] = [len(tableColumnNames[i])
                           for i in range(len(tableColumnNames))]

        output = io.BytesIO()
        # setup csv styles
        if csvType == "machine":
            delimiter = ","
        elif csvType in ["human", "markdown"]:
            delimiter = "|"
        elif csvType in ["reST"]:
            delimiter = "|"
        if csvType in ["markdown"]:
            writer = csv.writer(output, delimiter=delimiter,
                                quoting=csv.QUOTE_NONE, doublequote=False, quotechar='"', escapechar="\\", lineterminator="\n")
        else:
            writer = csv.writer(output, dialect='excel', delimiter=delimiter,
                                quotechar='"', quoting=csv.QUOTE_MINIMAL, lineterminator="\n")

        if csvType in ["markdown"]:
            dividerWriter = csv.writer(
                output, delimiter="|", quoting=csv.QUOTE_NONE, doublequote=False, quotechar='"', escapechar="\\", lineterminator="\n")
        else:
            dividerWriter = csv.writer(output, dialect='excel', delimiter="+",
                                       quotechar='"', quoting=csv.QUOTE_MINIMAL, lineterminator="\n")
        # add column names to csv
        header = []
        divider = []
        rstDivider = []
        allRows = []

        # clean up data
        for row in dataCopy:
            for c in tableColumnNames:
                if isinstance(row[c], float) or isinstance(row[c], Decimal):
                    row[c] = "%0.9g" % row[c]
                elif isinstance(row[c], datetime):
                    thisDate = str(row[c])[:10]
                    row[c] = "%(thisDate)s" % locals()

        # set the column widths
        for row in dataCopy:
            for i, c in enumerate(tableColumnNames):
                if len(unicode(row[c])) > columnWidths[i]:
                    columnWidths[i] = len(unicode(row[c]))

        # table borders for human readable
        if csvType in ["human", "markdown", "reST"]:
            header.append("")
            divider.append("")
            rstDivider.append("")

        for i, c in enumerate(tableColumnNames):
            if csvType == "machine":
                header.append(c)
            elif csvType in ["human", "markdown", "reST"]:
                header.append(
                    c.ljust(columnWidths[i] + 2).rjust(columnWidths[i] + 3))
                divider.append('-' * (columnWidths[i] + 3))
                rstDivider.append('=' * (columnWidths[i] + 3))

        # table border for human readable
        if csvType in ["human", "markdown", "reST"]:
            header.append("")
            divider.append("")
            rstDivider.append("")

        # fill in the data
        for row in dataCopy:
            thisRow = []
            # table border for human readable
            if csvType in ["human", "markdown", "reST"]:
                thisRow.append("")

            for i, c in enumerate(tableColumnNames):
                if csvType in ["human", "markdown", "reST"]:
                    if row[c] == None:
                        row[c] = ""
                    row[c] = unicode(unicode(row[c]).ljust(columnWidths[i] + 2)
                                     .rjust(columnWidths[i] + 3))
                thisRow.append(row[c])
            # table border for human readable
            if csvType in ["human", "markdown", "reST"]:
                thisRow.append("")
            allRows.append(thisRow)
            if csvType in ["reST"]:
                allRows.append(divider)

        if csvType == "machine":
            writer.writerow(header)
        if csvType in ["reST"]:
            dividerWriter.writerow(divider)
            writer.writerow(header)
            dividerWriter.writerow(rstDivider)
        if csvType in ["human"]:
            dividerWriter.writerow(divider)
            writer.writerow(header)
            dividerWriter.writerow(divider)
        elif csvType in ["markdown"]:
            writer.writerow(header)
            dividerWriter.writerow(divider)

        # write out the data
        writer.writerows(allRows)
        # table border for human readable
        if csvType in ["human"]:
            dividerWriter.writerow(divider)

        output = output.getvalue()
        output = output.strip()

        if csvType in ["markdown"]:
            output = output.replace("|--", "|:-")
        if csvType in ["reST"]:
            output = output.replace("|--", "+--").replace("--|", "--+")

        self.log.debug(
            'completed the ``_list_of_dictionaries_to_csv`` function')

        return output
def _list_of_dictionaries_to_csv ( self , csvType = "human" ) : self . log . debug ( 'starting the ``_list_of_dictionaries_to_csv`` function' ) if not len ( self . listOfDictionaries ) : return "NO MATCH" dataCopy = copy . deepcopy ( self . listOfDictionaries ) tableColumnNames = dataCopy [ 0 ] . keys ( ) columnWidths = [ ] columnWidths [ : ] = [ len ( tableColumnNames [ i ] ) for i in range ( len ( tableColumnNames ) ) ] output = io . BytesIO ( ) if csvType == "machine" : delimiter = "," elif csvType in [ "human" , "markdown" ] : delimiter = "|" elif csvType in [ "reST" ] : delimiter = "|" if csvType in [ "markdown" ] : writer = csv . writer ( output , delimiter = delimiter , quoting = csv . QUOTE_NONE , doublequote = False , quotechar = '"' , escapechar = "\\" , lineterminator = "\n" ) else : writer = csv . writer ( output , dialect = 'excel' , delimiter = delimiter , quotechar = '"' , quoting = csv . QUOTE_MINIMAL , lineterminator = "\n" ) if csvType in [ "markdown" ] : dividerWriter = csv . writer ( output , delimiter = "|" , quoting = csv . QUOTE_NONE , doublequote = False , quotechar = '"' , escapechar = "\\" , lineterminator = "\n" ) else : dividerWriter = csv . writer ( output , dialect = 'excel' , delimiter = "+" , quotechar = '"' , quoting = csv . QUOTE_MINIMAL , lineterminator = "\n" ) header = [ ] divider = [ ] rstDivider = [ ] allRows = [ ] for row in dataCopy : for c in tableColumnNames : if isinstance ( row [ c ] , float ) or isinstance ( row [ c ] , Decimal ) : row [ c ] = "%0.9g" % row [ c ] elif isinstance ( row [ c ] , datetime ) : thisDate = str ( row [ c ] ) [ : 10 ] row [ c ] = "%(thisDate)s" % locals ( ) for row in dataCopy : for i , c in enumerate ( tableColumnNames ) : if len ( unicode ( row [ c ] ) ) > columnWidths [ i ] : columnWidths [ i ] = len ( unicode ( row [ c ] ) ) if csvType in [ "human" , "markdown" , "reST" ] : header . append ( "" ) divider . append ( "" ) rstDivider . append ( "" ) for i , c in enumerate ( tableColumnNames ) : if csvType == "machine" : header . append ( c ) elif csvType in [ "human" , "markdown" , "reST" ] : header . append ( c . ljust ( columnWidths [ i ] + 2 ) . rjust ( columnWidths [ i ] + 3 ) ) divider . append ( '-' * ( columnWidths [ i ] + 3 ) ) rstDivider . append ( '=' * ( columnWidths [ i ] + 3 ) ) if csvType in [ "human" , "markdown" , "reST" ] : header . append ( "" ) divider . append ( "" ) rstDivider . append ( "" ) for row in dataCopy : thisRow = [ ] if csvType in [ "human" , "markdown" , "reST" ] : thisRow . append ( "" ) for i , c in enumerate ( tableColumnNames ) : if csvType in [ "human" , "markdown" , "reST" ] : if row [ c ] == None : row [ c ] = "" row [ c ] = unicode ( unicode ( row [ c ] ) . ljust ( columnWidths [ i ] + 2 ) . rjust ( columnWidths [ i ] + 3 ) ) thisRow . append ( row [ c ] ) if csvType in [ "human" , "markdown" , "reST" ] : thisRow . append ( "" ) allRows . append ( thisRow ) if csvType in [ "reST" ] : allRows . append ( divider ) if csvType == "machine" : writer . writerow ( header ) if csvType in [ "reST" ] : dividerWriter . writerow ( divider ) writer . writerow ( header ) dividerWriter . writerow ( rstDivider ) if csvType in [ "human" ] : dividerWriter . writerow ( divider ) writer . writerow ( header ) dividerWriter . writerow ( divider ) elif csvType in [ "markdown" ] : writer . writerow ( header ) dividerWriter . writerow ( divider ) writer . writerows ( allRows ) if csvType in [ "human" ] : dividerWriter . writerow ( divider ) output = output . getvalue ( ) output = output . strip ( ) if csvType in [ "markdown" ] : output = output . replace ( "|--" , "|:-" ) if csvType in [ "reST" ] : output = output . replace ( "|--" , "+--" ) . replace ( "--|" , "--+" ) self . log . debug ( 'completed the ``_list_of_dictionaries_to_csv`` function' ) return output
Convert a python list of dictionaries to pretty csv output

        **Key Arguments:**
            - ``csvType`` -- human, machine or reST

        **Return:**
            - ``output`` -- the contents of a CSV file