Ejemplo n.º 1
0
def csv_mapper(fileobj, mapping, resolver=None, scope=None):
    """ Given a CSV file object (fh), parse the file as a unicode CSV document,
    iterate over all rows of the data and map them to a JSON schema using the
    mapping instructions in ``mapping``. """
    from jsonmapping import Mapper
    reader = unicodecsv.DictReader(fileobj)
    for row in Mapper.apply_iter(reader, mapping, resolver=resolver,
                                 scope=scope):
        yield row
Ejemplo n.º 2
0
def csv_mapper(fileobj, mapping, resolver=None, scope=None):
    """ Given a CSV file object (fh), parse the file as a unicode CSV document,
    iterate over all rows of the data and map them to a JSON schema using the
    mapping instructions in ``mapping``. """
    reader = unicodecsv.DictReader(fileobj)
    for (row, err) in Mapper.apply_iter(reader,
                                        mapping,
                                        resolver=resolver,
                                        scope=scope):
        yield (row, err)
Ejemplo n.º 3
0
def load_mapped_csv(graph, csv_uri, mapping, context_id=None):
    """ Load data from a CSV file, applying a JSON mapping and then adding
    it to the graph. """
    meta = {'source_url': csv_uri}
    reader = unicodecsv.DictReader(read_uri(csv_uri))
    ctx = graph.context(identifier=context_id, meta=meta)
    for data, err in Mapper.apply_iter(reader, mapping, graph.resolver,
                                       scope=graph.base_uri):
        if err is not None:
            log.warning("Error loading %r: %r", csv_uri, err)
        else:
            ctx.add(data['$schema'], data)
    ctx.save()
Ejemplo n.º 4
0
def load_mapped_csv(graph, csv_uri, mapping, context_id=None):
    """ Load data from a CSV file, applying a JSON mapping and then adding
    it to the graph. """
    meta = {'source_url': csv_uri}
    reader = unicodecsv.DictReader(read_uri(csv_uri))
    ctx = graph.context(identifier=context_id, meta=meta)
    for data, err in Mapper.apply_iter(reader,
                                       mapping,
                                       graph.resolver,
                                       scope=graph.base_uri):
        if err is not None:
            log.warning("Error loading %r: %r", csv_uri, err)
        else:
            ctx.add(data['$schema'], data)
    ctx.save()