BurntSushi / xsv

A fast CSV command line toolkit written in Rust.
The Unlicense
10.23k stars 317 forks source link

xsv table displays values with escaped `"`s incorrectly #337

Open Kinrany opened 5 months ago

Kinrany commented 5 months ago

Example file:

header
a
"a"
""""

Expected output:

header
a
a
"

Real output:

header
a
a
""""
BurntSushi commented 3 months ago

xsv table doesn't print the parsed contents of CSV data. It just rewrites the input CSV as tab-delimited CSV, and then expands the tabs in the output as per elastic tabstops. This is easy to see in the implementation: https://github.com/BurntSushi/xsv/blob/4278b8593260bb2e9bfdf6e91cc4df5b7df5b8fb/src/cmd/table.rs#L49-L71

It does seem plausible that it would be better to print the parsed contents (i.e., after unescaping quotes).

Kinrany commented 3 months ago

The thing that confused me the most was that it does remove the quotes around "a".

Is the output of xsv table intended to be parseable as CSV too?

BurntSushi commented 3 months ago

The thing that confused me the most was that it does remove the quotes around "a".

Yes, because the CSV writer knows when quotes are necessary. They aren't necessary to write a.

Is the output of xsv table intended to be parseable as CSV too?

No. As I said, the tabs are expanded to spaces. So it's very deliberately not CSV. It's for human consumption. Which is arguably why this should be treated as a bug and the quotes should be unescaped.

Kinrany commented 3 months ago

That's what I thought!

Can I help? Xsv is a great tool 😄

vmchale commented 3 months ago

xsv table doesn't print the parsed contents of CSV data. It just rewrites the input CSV as tab-delimited CSV, and then expands the tabs in the output as per elastic tabstops. This is easy to see in the implementation:

https://github.com/BurntSushi/xsv/blob/4278b8593260bb2e9bfdf6e91cc4df5b7df5b8fb/src/cmd/table.rs#L49-L71

It does seem plausible that it would be better to print the parsed contents (i.e., after unescaping quotes).

I ran into something similar for xsv fmt --ascii. Printing the parsed contents would be much more useful of a feature; one turns to ASV precisely so that you don't need to escape strings!

Kinrany commented 3 months ago

xsv fmt --table? 🤔

BurntSushi commented 3 months ago

xsv table doesn't print the parsed contents of CSV data. It just rewrites the input CSV as tab-delimited CSV, and then expands the tabs in the output as per elastic tabstops. This is easy to see in the implementation: https://github.com/BurntSushi/xsv/blob/4278b8593260bb2e9bfdf6e91cc4df5b7df5b8fb/src/cmd/table.rs#L49-L71

It does seem plausible that it would be better to print the parsed contents (i.e., after unescaping quotes).

I ran into something similar for xsv fmt --ascii. Printing the parsed contents would be much more useful of a feature; one turns to ASV precisely so that you don't need to escape strings!

xsv fmt --ascii should definitely not print the parsed contents. It should print CSV data. And insert quoting when necessary. But it does look like it is inserting quotes when it shouldn't for --ascii.