Convert csv to dict

    :param delimiter:
    :param fields:
    :return:
181111
def csv_to_map(fields, delimiter=','):
    

    def _csv_to_list(csv_input):
        """
        Util function to overcome the use of files by in-memory io buffer

        :param csv_input:
        :return:
        """
        io_file = io.StringIO(csv_input)
        return next(csv.reader(io_file, delimiter=delimiter))

    def _app(current_tuple, e=None):
        if current_tuple is None or len(current_tuple) == 0:
            return None, "no input"
        csv_list = _csv_to_list(current_tuple)
        if len(csv_list) != len(fields):
            e = {"input": "unexpected number of fields {} obtained {} expected".format(len(csv_list), len(fields))}
            return None, e
        return {k: v for (k, v) in zip(fields, csv_list)}, e
    if fields is None or len(fields) == 0:
        return fixed_input(None, "no fields provided, cannot proceed without order")
    return _app
785930
def csv_dict(d):
    
    if len(d) == 0:
        return "{}"
    return "{" + ', '.join(["'{}': {}".format(k, quotable(v))
                            for k, v in d.items()]) + "}"
84318
public static function csvFields(string $line, string $delimiter = ','): array
    {
        if (trim($line) === '') {
            return [];
        }
        $fields = [];
        $parts = str_getcsv($line, $delimiter);
        if ($parts !== null && count($parts) > 0) {
            foreach ($parts as $part) {
                $part = trim($part);
                if (substr($part, 0, 1) === '"' || substr($part, 0, 1) === '\'') {
                    $part = substr($part, 1);
                }
                if (substr($part, -1) === '"' || substr($part, -1) === '\'') {
                    $part = substr($part, 0, -1);
                }
                if ($part !== '') {
                    $fields[] = $part;
                }
            }
        }
        return $fields;
    }
1192650
def _fields_list_to_dict(fields):
    
    for key in fields:
        assert isinstance(key, (str,unicode))
    return dict([[key, 1] for key in fields])
4674
def field_dict(self, model):
        
        if self.field_names is None:
            return dict([(f.name, f) for f in model._meta.fields
                         if isinstance(f, models.DateField)])
        else:
            return dict([(f.name, f)
                         for f in model._meta.fields
                         if isinstance(f, models.DateField) and
                            (f.name in self.field_names)])
1343759
def splitterfields(data, commdct):
    """"""
    objkey = "Connector:Splitter".upper()
    fieldlists = splittermixerfieldlists(data, commdct, objkey)
    return extractfields(data, commdct, objkey, fieldlists)
810651
def json_to_csv(json_input):
    '''
    
    '''
    try:
        json_input = json.loads(json_input)
    except:
        pass # If loads fails, it's probably already parsed
    headers = set()
    for json_row in json_input:
        headers.update(json_row.keys())

    csv_io = StringIO.StringIO()
    csv_out = csv.DictWriter(csv_io,headers)
    csv_out.writeheader()
    for json_row in json_input:
        csv_out.writerow(json_row)
    csv_io.seek(0)
    return csv_io.read()
57433
def _dictify(field, value):
    
    if value is None:
        return None
    elif field.type_:
        return value.to_dict()
    return field.dict_value(value)
1149812
def obj_to_dict(cls, obj):
        
        data = {}
        for field_name in cls.get_fields():
            try:
                value = getattr(obj, field_name)
            except AttributeError:
                # If the field doesn't exist on the object, fail gracefully
                # and don't include the field in the data dict at all. Fail
                # loudly if the field exists but produces a different error
                # (edge case: accessing an *existing* field could technically
                # produce an unrelated AttributeError).
                continue

            if callable(value):
                value = value()
            data[field_name] = value

        return data
181832
def dump_csv(data: List[dict], fieldnames: Sequence[str], with_header: bool = False, crlf: bool = False,
             tsv: bool = False) -> str:
    

    def force_str(v):
        # XXX: Double quotation behaves strangely... so replace (why?)
        return dump_json(v).replace('"', "'") if isinstance(v, (dict, list)) else v

    with io.StringIO() as sio:
        dialect = get_dialect_name(crlf, tsv)
        writer = csv.DictWriter(sio, fieldnames=fieldnames, dialect=dialect, extrasaction='ignore')
        if with_header:
            writer.writeheader()
        for x in data:
            writer.writerow({k: force_str(v) for k, v in x.items()})
        sio.seek(0)
        return sio.read()
1211183
def _FormatField(self, field):
    
    if self._FIELD_DELIMITER and isinstance(field, py2to3.STRING_TYPES):
      return field.replace(self._FIELD_DELIMITER, ' ')
    return field
875380
def _line_to_dict(self, file, field_names):
        
        for line in file:
            delimiter = settings.IPGEOBASE_FILE_FIELDS_DELIMITER
            yield self._extract_data_from_line(line, field_names, delimiter)
971565
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()
        # 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
1230723
def _normalize_coerce_to_field_dict(self, v):
        

        def tokenize(s):
            """ Tokenize a string by splitting it by + and -

            >>> tokenize('this + that')
            ['this', '+', 'that']

            >>> tokenize('this+that')
            ['this', '+', 'that']

            >>> tokenize('this+that-other')
            ['this', '+', 'that', '-', 'other]
            """

            # Crude tokenization
            s = s.replace('+', ' + ').replace('-', ' - ') \
                .replace('/', ' / ').replace('*', ' * ')
            words = [w for w in s.split(' ') if w]
            return words

        if isinstance(v, _str_type):
            field_parts = tokenize(v)
            field = field_parts[0]
            d = {'value': field}
            if len(field_parts) > 1:
                # if we need to add and subtract from the field join the field
                # parts into pairs, for instance if field parts is
                #     [MyTable.first, '-', MyTable.second, '+', MyTable.third]
                # we will get two pairs here
                #     [('-', MyTable.second), ('+', MyTable.third)]
                d['operators'] = []
                for operator, other_field in zip(
                    field_parts[1::2], field_parts[2::2]
                ):
                    d['operators'].append({
                        'operator': operator,
                        'field': {
                            'value': other_field
                        }
                    })
            return d
        else:
            return v
770479
def _convert_list2str(self, fields):
        
        if isinstance(fields, tuple) or isinstance(fields, list):
            return ','.join(fields)
        return fields
359834
def fields_to_dict(fields, type_=OrderedDict):
    """"""
    fields_iterator = iter(fields)
    return type_(zip(fields_iterator, fields_iterator))
1067946
def itersplit_to_fields(self, _str):
        
        if self.preparse:
            _str = self.preparse(_str)

        _fields = itersplit(_str, self.fsep)

        try:
            values = (
                t[1] for t in izip_longest(self._tuple._fields, _fields))
            return self._tuple(*values)
        except Exception as e:
            log.error(self._tuple)
            log.error(_fields)
            log.exception(e)
            raise