[docs]def_looks_like_existing_path(source:str)->bool:try:returnPath(source).exists()exceptOSError:# Inline payloads can be much longer than filesystem path limits.returnFalse
[docs]defload_tabular_rows(source:str|bytes,max_rows:int|None=None)->list[dict[str,Any]]:# Excel's "CSV UTF-8" starts with a byte-order mark; utf-8-sig and the# inline strip below drop it so it isn't read into the first column name.rows:list[dict[str,Any]]=[]ifisinstance(source,bytes):withio.StringIO(source.decode("utf-8-sig"))ashandle:reader=csv.DictReader(handle)forindex,rowinenumerate(reader):rows.append(dict(row))ifmax_rowsisnotNoneandindex+1>=max_rows:breakreturnrowsif_looks_like_existing_path(source):withPath(source).open("r",encoding="utf-8-sig",newline="")ashandle:reader=csv.DictReader(handle)forindex,rowinenumerate(reader):rows.append(dict(row))ifmax_rowsisnotNoneandindex+1>=max_rows:breakelse:withio.StringIO(source.removeprefix("\ufeff"))ashandle:reader=csv.DictReader(handle)forindex,rowinenumerate(reader):rows.append(dict(row))ifmax_rowsisnotNoneandindex+1>=max_rows:breakreturnrows