From e225054a167086b97923a8449fd0af013ce26933 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Thu, 24 Feb 2022 17:13:17 +0300 Subject: Create new method for filling csv with a default value * gn3/csvcmp.py (fill_csv): Given a csv text with uneven or incomplete fields whole length are less than width, fill them with a value which defaults to "x". * tests/unit/test_csvcmp.py (test_fill_csv): Test cases for the above. --- gn3/csvcmp.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'gn3') diff --git a/gn3/csvcmp.py b/gn3/csvcmp.py index 35133dc..7a4a026 100644 --- a/gn3/csvcmp.py +++ b/gn3/csvcmp.py @@ -67,3 +67,14 @@ def csv_diff(base_csv, delta_csv, tmp_dir="/tmp"): if os.path.exists(file_name2): os.remove(file_name2) return _r + + +def fill_csv(csv_text, width, value="x"): + data = [] + for line in csv_text.strip().split("\n"): + if line.startswith("Strain") or line.startswith("#"): + data.append(line) + elif line: + data.append( + ",".join((_n:=line.split(",")) + [value] * (width - len(_n)))) + return "\n".join(data) -- cgit v1.2.3