mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-29 05:14:28 +02:00
add io.buffer in _extra_key_strategy to ensure is closed in the end
This commit is contained in:
parent
b006a4c685
commit
d3f097901c
1 changed files with 7 additions and 1 deletions
|
|
@ -212,6 +212,7 @@ def _extra_key_strategy(
|
||||||
reader: Iterable[dict],
|
reader: Iterable[dict],
|
||||||
ignore_extras: Optional[bool] = False,
|
ignore_extras: Optional[bool] = False,
|
||||||
extras_key: Optional[str] = None,
|
extras_key: Optional[str] = None,
|
||||||
|
buffer: Optional[io.TextIOWrapper] = None,
|
||||||
) -> Iterable[dict]:
|
) -> Iterable[dict]:
|
||||||
# Logic for handling CSV rows with more values than there are headings
|
# Logic for handling CSV rows with more values than there are headings
|
||||||
for row in reader:
|
for row in reader:
|
||||||
|
|
@ -231,6 +232,8 @@ def _extra_key_strategy(
|
||||||
else:
|
else:
|
||||||
row[extras_key] = row.pop(None) # type: ignore
|
row[extras_key] = row.pop(None) # type: ignore
|
||||||
yield row
|
yield row
|
||||||
|
if buffer:
|
||||||
|
buffer.close()
|
||||||
|
|
||||||
|
|
||||||
def rows_from_file(
|
def rows_from_file(
|
||||||
|
|
@ -299,7 +302,10 @@ def rows_from_file(
|
||||||
reader = csv.DictReader(decoded_fp, dialect=dialect)
|
reader = csv.DictReader(decoded_fp, dialect=dialect)
|
||||||
else:
|
else:
|
||||||
reader = csv.DictReader(decoded_fp)
|
reader = csv.DictReader(decoded_fp)
|
||||||
return _extra_key_strategy(reader, ignore_extras, extras_key), Format.CSV
|
return (
|
||||||
|
_extra_key_strategy(reader, ignore_extras, extras_key, decoded_fp),
|
||||||
|
Format.CSV,
|
||||||
|
)
|
||||||
elif format == Format.TSV:
|
elif format == Format.TSV:
|
||||||
rows = rows_from_file(
|
rows = rows_from_file(
|
||||||
fp, format=Format.CSV, dialect=csv.excel_tab, encoding=encoding
|
fp, format=Format.CSV, dialect=csv.excel_tab, encoding=encoding
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue