petl-developers / petl

Python Extract Transform and Load Tables of Data
MIT License
1.22k stars 190 forks source link

tocsv(source=None) does not output to stdout #632

Closed blais closed 1 year ago

blais commented 1 year ago

Minimal, reproducible code sample, a copy-pastable example if possible


petl.wrap([...]).tocsv()

Problem description

The documentation says

Load - writing tables to files and databases
The following functions write data from a table to a file-like source or database. For functions that accept a source argument, if the source argument is None or a string it is interpreted as follows:

None - write to stdout
...

I've never found this to work, and always work around it. I looked into it this time briefly, and it looks like a blatant oversight.

The _resolve_source_from_arg function gets called from both the read_source_from_arg() and write_source_from_arg() functions below it and does this:

def _resolve_source_from_arg(source, handlers):
    if source is None:
        return StdinSource()
   ...

You need to move the conditional within each of the two functions that call it (simple solution).

Probably should be prioritized high... this looks pretty bad for newcomers to the library, as this is library one of the first things someone might do.

Version and installation information

1.7.11