From 5e6a5068cff71b3b30525d0a12868bdf6551456c Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Thu, 24 Mar 2022 11:57:01 -0700 Subject: [PATCH] allow iterables for simple table parser --- jc/parsers/universal.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/jc/parsers/universal.py b/jc/parsers/universal.py index 81aa14f0..b9f392e2 100644 --- a/jc/parsers/universal.py +++ b/jc/parsers/universal.py @@ -2,7 +2,7 @@ from typing import Iterable, List, Dict -def simple_table_parse(data: List[str]) -> List[Dict]: +def simple_table_parse(data: Iterable[str]) -> List[Dict]: """ Parse simple tables. There should be no blank cells. The last column may contain data with spaces. @@ -22,7 +22,7 @@ def simple_table_parse(data: List[str]) -> List[Dict]: Parameters: - data: (list) Text data to parse that has been split into lines + data: (iter) Text data to parse that has been split into lines via .splitlines(). Item 0 must be the header row. Any spaces in header names should be changed to underscore '_'. You should also ensure headers are @@ -37,6 +37,10 @@ def simple_table_parse(data: List[str]) -> List[Dict]: """ # code adapted from Conor Heine at: # https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501 + + # cast iterable to a list. Also keeps from mutating the caller's list + data = list(data) + headers = [h for h in ' '.join(data[0].strip().split()).split() if h] raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), data[1:]) raw_output = [dict(zip(headers, r)) for r in raw_data]