mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-26 19:04:32 +02:00
WIP --extras-key --ignore-extras - does not work yet, refs #444
This commit is contained in:
parent
b8af3b96f5
commit
e52925bbb9
3 changed files with 88 additions and 3 deletions
|
|
@ -9,7 +9,7 @@ import json
|
|||
import os
|
||||
import sys
|
||||
from . import recipes
|
||||
from typing import Dict, cast, BinaryIO, Iterable, Optional, Tuple, Type
|
||||
from typing import Any, Dict, cast, BinaryIO, Iterable, Optional, Tuple, Type
|
||||
|
||||
import click
|
||||
|
||||
|
|
@ -219,6 +219,25 @@ def _extra_key_strategy(
|
|||
yield row
|
||||
|
||||
|
||||
def _extra_key_strategy_row(
|
||||
headers: Iterable[str],
|
||||
row: Iterable[Any],
|
||||
ignore_extras: Optional[bool] = False,
|
||||
extras_key: Optional[str] = None,
|
||||
) -> dict:
|
||||
# Logic for handling CSV rows with more values than there are headings
|
||||
if len(headers) <= len(row) or ignore_extras:
|
||||
return dict(zip(headers, row))
|
||||
# There are more row items than headers, what to do with the extras?
|
||||
if not ignore_extras:
|
||||
extras = row[len(headers) :]
|
||||
raise RowError("Row {} contained these extra values: {}".format(row, extras))
|
||||
if extras_key:
|
||||
d = dict(zip(headers, row))
|
||||
d[extras_key] = row[len(headers) :]
|
||||
return d
|
||||
|
||||
|
||||
def rows_from_file(
|
||||
fp: BinaryIO,
|
||||
format: Optional[Format] = None,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue