How to use the jsonlines.Reader function in jsonlines

To help you get started, we’ve selected a few jsonlines examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wbolster / jsonlines / tests / test_jsonlines.py View on Github external
def test_reading_from_iterable():
    with jsonlines.Reader(['1', b'{}']) as reader:
        assert list(reader) == [1, {}]
    assert 'wrapping 
github wbolster / jsonlines / tests / test_jsonlines.py View on Github external
def test_invalid_lines():
    data = u'[1, 2'
    with jsonlines.Reader(io.StringIO(data)) as reader:
        with pytest.raises(jsonlines.InvalidLineError) as excinfo:
            reader.read()
        exc = excinfo.value
        assert "invalid json" in str(exc)
        assert exc.line == data
github wbolster / jsonlines / tests / test_jsonlines.py View on Github external
def test_reader():
    fp = io.BytesIO(SAMPLE_BYTES)
    with jsonlines.Reader(fp) as reader:
        it = iter(reader)
        assert next(it) == {'a': 1}
        assert next(it) == {'b': 2}
        with pytest.raises(StopIteration):
            next(it)
        with pytest.raises(EOFError):
            reader.read()
github wbolster / jsonlines / tests / test_jsonlines.py View on Github external
def test_invalid_utf8():
    with jsonlines.Reader([b'\xff\xff']) as reader:
        with pytest.raises(jsonlines.InvalidLineError) as excinfo:
            reader.read()
        assert 'line is not valid utf-8' in str(excinfo.value)
github wbolster / jsonlines / tests / test_jsonlines.py View on Github external
def test_empty_strings_in_iterable():
    input = ['123', '', '456']
    it = iter(jsonlines.Reader(input))
    assert next(it) == 123
    with pytest.raises(jsonlines.InvalidLineError):
        next(it)
    with pytest.raises(StopIteration):
        next(it)
    it = jsonlines.Reader(input).iter(skip_empty=True)
    assert list(it) == [123, 456]
github alexwarstadt / data_generation / results_processing / IMPPRES / unify_nli_results.py View on Github external
def unify_results(results_file, data_file):
    results = np.load(os.path.join(presuppositions_dir, results_file))
    data = [l for l in jsonlines.Reader(open(os.path.join(data_dir, data_file))).iter()]
    for r, d in zip(results, data):
        d["pred_entailment"] = r[0]
        d["pred_neutral"] = r[1]
        d["pred_contradiction"] = r[2]
    return data
github frictionlessdata / tabulator-py / tabulator / parsers / ndjson.py View on Github external
def __iter_extended_rows(self):
        rows = jsonlines.Reader(self.__chars)
        for row_number, row in enumerate(rows, start=1):
            if isinstance(row, (tuple, list)):
                yield row_number, None, list(row)
            elif isinstance(row, dict):
                keys, values = zip(*sorted(row.items()))
                yield (row_number, list(keys), list(values))
            else:
                if not self.__force_parse:
                    raise exceptions.SourceError('JSON item has to be list or dict')
                yield (row_number, None, [])
github quiltdata / quilt / api / python / quilt3 / packages.py View on Github external
def _load(cls, readable_file):
        reader = jsonlines.Reader(readable_file)
        meta = reader.read()
        meta.pop('top_hash', None)  # Obsolete as of PR #130
        pkg = cls()
        pkg._meta = meta
        for obj in reader:
            path = cls._split_key(obj.pop('logical_key'))
            subpkg = pkg._ensure_subpackage(path[:-1])
            key = path[-1]
            if not obj.get('physical_keys', None):
                # directory-level metadata
                subpkg.set_meta(obj['meta'])
                continue
            if key in subpkg._children:
                raise PackageException("Duplicate logical key while loading package")
            subpkg._children[key] = PackageEntry(
                obj['physical_keys'][0],

jsonlines

Library with helpers for the jsonlines file format

BSD-2-Clause
Latest version published 1 year ago

Package Health Score

68 / 100
Full package analysis

Similar packages