ruby / csv

CSV Reading and Writing
https://ruby.github.io/csv/
BSD 2-Clause "Simplified" License
178 stars 113 forks source link

Add quoted information to CSV::FieldInfo #252

Closed kou closed 2 years ago

kou commented 2 years ago

Some CSV writers use quote with special meaning. For example, R's write.csv use quote for string value or factor value.

https://www.rdocumentation.org/packages/utils/versions/3.6.2/topics/write.table

quote

a logical value (TRUE or FALSE) or a numeric vector. If TRUE, any character or factor columns will be surrounded by double quotes. If a numeric vector, its elements are taken as the indices of columns to quote. In both cases, row and column names are quoted if they are written. If FALSE, nothing is quoted.

The current CSV parser doesn't provide quoted information to field converter. If field convert knows whether the target value is quoted or not, it can process the target value as a factor value.

See also: https://github.com/red-data-tools/red-datasets/pull/140#issuecomment-1164855481

heronshoes commented 2 years ago

:+1:

For example,

str = <<~CSV
  "serial","value"
  "108",11
  "109",12
  "10A",13
  "10B",14
CSV
CSV.parse(str, converters: [:numeric])

# =>
[["serial", "value"], [108, 11], [109, 12], ["10A", 13], ["10B", 14]]

I want to preserve quoted numeric values "108", "109", but there are no means to do this in converter.