File tree Expand file tree Collapse file tree 1 file changed +35
-3
lines changed
Expand file tree Collapse file tree 1 file changed +35
-3
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments