Coverage for portality/lib/csv_utils.py: 72%

18 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 13:43 +0000

1import csv 

2from typing import Iterable, Union 

3 

4 

5def read_all(csv_path, as_dict=False) -> Iterable[Union[list, dict]]: 

6 reader = csv.DictReader if as_dict else csv.reader 

7 with open(csv_path, 'r') as f: 

8 for row in reader(f): 

9 yield row 

10 

11 

12def is_empty_row(row: dict) -> bool: 

13 """ True if every cell in a DictReader row is blank, including any overflow cells stored under the None key """ 

14 for v in row.values(): 

15 cells = v if isinstance(v, list) else [v] 

16 if any(c is not None and c.strip() for c in cells): 

17 return False 

18 return True 

19 

20 

21def trim_trailing_empty_rows(rows: Iterable[dict]) -> list: 

22 """ Read all rows, discarding any empty rows at the end (e.g. spreadsheet exports padded with blank lines) """ 

23 rows = list(rows) 

24 while rows and is_empty_row(rows[-1]): 

25 rows.pop() 

26 return rows