@@ -376,7 +376,7 @@ Define new commands in your config file which provide aliases to other commands.
376376 [[alias]]
377377
378378 name = "jsonl"
379- short_help = "Load jsonlines into python objects."
379+ help = "Load jsonlines into python objects."
380380
381381 [[alias.stage]]
382382
@@ -419,7 +419,7 @@ Convenient for removing trailing commas.
419419 [[alias]]
420420
421421 name = "yml2json"
422- short_help = "Convert yaml to json"
422+ help = "Convert yaml to json"
423423
424424 [[alias.stage]]
425425
@@ -455,7 +455,7 @@ Pull text out of xml documents.
455455
456456 [[alias]]
457457 name="xpath"
458- short_help = "Find xml elements matching xpath query."
458+ help = "Find xml elements matching xpath query."
459459 arguments = [{name="query", type="str"}]
460460 inject_values=["query"]
461461
@@ -482,7 +482,7 @@ Generate json objects
482482
483483
484484 name="jo"
485- short_help ="Make json objects"
485+ help ="Make json objects"
486486 arguments=[{name="pairs", type="str"}]
487487 inject_values=["pairs"]
488488
@@ -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"
540- short_help = "Load csv rows into python objects "
541- inject_values=["delimiter"]
566+ help = "Load csv rows into python dicts. With --no-header, keys will be numbered from 0. "
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