Skip to content

Commit b8af9d3

Browse files
akbakb
authored andcommitted
Add --header
1 parent 701ddd1 commit b8af9d3

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

README.rst

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,25 +535,57 @@ try:
535535
536536
.. code-block:: toml
537537
538+
base_exec_before = '''
539+
import csv
540+
import typing as t
541+
542+
543+
def read_csv(
544+
file, header: bool, **kwargs
545+
) -> t.Iterable[t.Dict[t.Union[str, int], str]]:
546+
"Read csv rows into an iterable of dicts."
547+
548+
rows = list(file)
549+
550+
first_row = next(csv.reader(rows))
551+
if header:
552+
fieldnames = first_row
553+
reader = csv.DictReader(rows, fieldnames=fieldnames, **kwargs)
554+
return list(reader)[1:]
555+
556+
fieldnames = range(len(first_row))
557+
return csv.DictReader(rows, fieldnames=fieldnames, **kwargs)
558+
559+
'''
560+
561+
562+
563+
538564
[[alias]]
539565
name = "csv"
540566
short_help = "Load csv rows into python objects"
541-
inject_values=["delimiter"]
567+
inject_values=["delimiter", "header"]
542568
543569
[[alias.options]]
544570
name = "--delimiter"
545571
default = ","
572+
help = "field delimiter character"
573+
574+
[[alias.options]]
575+
name = "--header/--no-header"
576+
default=true
577+
help = "Treat the first row as a header?"
546578
547579
[[alias.stage]]
548580
command = "apply"
549-
options = {code="csv.DictReader(x, delimiter=delimiter)"}
581+
options = {code="read_csv(x, header=header)"}
550582
551583
[[alias.stage]]
552584
command = "chain"
553585
554586
[[alias.stage]]
555587
command = "map"
556-
options = {code="dict"}
588+
options = {code="dict(x)"}
557589
558590
559591

0 commit comments

Comments
 (0)