From bf4825d392c6369492978cb4a1eede439fa29891 Mon Sep 17 00:00:00 2001 From: Gabriel Altay Date: Mon, 7 Mar 2022 21:54:18 -0500 Subject: [PATCH] Modify dump_entities_to_json method (#33) (#34) * Modify dump_entities_to_json method * Minor version bump to v0.4.1 Co-authored-by: Kunal Tawatia --- qwikidata/__init__.py | 2 +- qwikidata/utils.py | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/qwikidata/__init__.py b/qwikidata/__init__.py index f001e4e..04a23f5 100644 --- a/qwikidata/__init__.py +++ b/qwikidata/__init__.py @@ -2,4 +2,4 @@ """Metadata for this package.""" __package_name__ = "qwikidata" -__version__ = "0.4.0" +__version__ = "0.4.1" diff --git a/qwikidata/utils.py b/qwikidata/utils.py index f2b7684..9050b03 100644 --- a/qwikidata/utils.py +++ b/qwikidata/utils.py @@ -26,10 +26,13 @@ def dump_entities_to_json(entities: Iterable[WikidataEntity], out_fname: str) -> Output file name """ with open(out_fname, "w") as fp: - fp.write("[\n") - for ent_lo, ent_hi in pairwise(entities): - ent_str = json.dumps(ent_lo._entity_dict) - fp.write("{},\n".format(ent_str)) - ent_str = json.dumps(ent_hi._entity_dict) - fp.write("{}".format(ent_str)) + fp.write("[") + + ent = next(entities, None) + while ent: + ent_str = json.dumps(ent._entity_dict) + fp.write("\n{}".format(ent_str)) + ent = next(entities, None) + if ent: fp.write(",") + fp.write("\n]")