xsv icon indicating copy to clipboard operation
xsv copied to clipboard

xsv table displays values with escaped `"`s incorrectly

Open Kinrany opened this issue 2 years ago • 7 comments

Example file:

header
a
"a"
""""

Expected output:

header
a
a
"

Real output:

header
a
a
""""

Kinrany avatar Feb 06 '24 22:02 Kinrany

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).

BurntSushi avatar Mar 26 '24 13:03 BurntSushi

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?

Kinrany avatar Mar 26 '24 19:03 Kinrany

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.

BurntSushi avatar Mar 26 '24 19:03 BurntSushi

That's what I thought!

Can I help? Xsv is a great tool 😄

Kinrany avatar Mar 26 '24 22:03 Kinrany

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!

vmchale avatar Mar 29 '24 22:03 vmchale

xsv fmt --table? 🤔

Kinrany avatar Mar 29 '24 23:03 Kinrany

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.

BurntSushi avatar Mar 29 '24 23:03 BurntSushi