Multiline fields and NeoCSVReader
Hi, I am having a csv file looking like "COL1";"COL2";"COL3";"DESCRIPTION" 1;"A string";13;"This is a long description spanning multiple lines, which is a challenge for most csv readers!" 2;"Another string";42;"" (CRLF line endings) Is there a combination of settings to NeoCSVReader that will allow to read such a file? Kind regards, Markus
Hi Markus, NeoCSVReader is not like most CSV readers ;-) (NeoCSVReader on: '"COL1";"COL2";"COL3";"DESCRIPTION" 1;"A string";13;"This is a long description spanning multiple lines, which is a challenge for most csv readers!" 2;"Another string";42;""' readStream) separator: $; ; upToEnd. => #( #('COL1' 'COL2' 'COL3' 'DESCRIPTION') #('1' 'A string' '13' 'This is a long description spanning multiple lines, which is a challenge for most csv readers!') #('2' 'Another string' '42' nil)) HTH, Sven PS: There are many features, like #readHeader and #skipHeader, and a whole, optional, mechanism to directly create your own objects and convert fields to specific types (like parsing numbers or dates). On 21 Sep 2014, at 20:24, Markus Fritsche <mfritsche@reauktion.de> wrote:
Hi,
I am having a csv file looking like
"COL1";"COL2";"COL3";"DESCRIPTION" 1;"A string";13;"This is a long description spanning multiple lines, which is a challenge for most csv readers!" 2;"Another string";42;""
(CRLF line endings)
Is there a combination of settings to NeoCSVReader that will allow to read such a file?
Kind regards, Markus
On 2014-09-21 20:52, Sven Van Caekenberghe wrote:
Hi Markus,
NeoCSVReader is not like most CSV readers
(NeoCSVReader on: '"COL1";"COL2";"COL3";"DESCRIPTION" 1;"A string";13;"This is a long description spanning multiple lines, which is a challenge for most csv readers!" 2;"Another string";42;""' readStream) separator: $; ; upToEnd.
Awesome. I must have been sleeping when I did my first test. Next question - if I might: is there a quick way to read the header and get the records read into dictionaries? Regards, Markus
On 21 Sep 2014, at 21:57, Markus Fritsche <mfritsche@reauktion.de> wrote:
On 2014-09-21 20:52, Sven Van Caekenberghe wrote:
Hi Markus,
NeoCSVReader is not like most CSV readers
(NeoCSVReader on: '"COL1";"COL2";"COL3";"DESCRIPTION" 1;"A string";13;"This is a long description spanning multiple lines, which is a challenge for most csv readers!" 2;"Another string";42;""' readStream) separator: $; ; upToEnd.
Awesome. I must have been sleeping when I did my first test.
Next question - if I might: is there a quick way to read the header and get the records read into dictionaries?
| reader | reader := NeoCSVReader on: 'A,B,C\1,2,3\3,4,5\6,7,8' withCRs readStream. reader readHeader do: [ :each | reader addFieldAt: each ]. reader recordClass: Dictionary. reader upToEnd. => an Array( a Dictionary('A'->'1' 'B'->'2' 'C'->'3' ) a Dictionary('A'->'3' 'B'->'4' 'C'->'5' ) a Dictionary('A'->'6' 'B'->'7' 'C'->'8' )) Is that quick enough ? You could also say, each asSymbol or use #addFieldAt:converter: Even shorter is to replace the second line by reader addFieldsAt: reader readHeader. Sven
Regards, Markus
participants (2)
-
Markus Fritsche -
Sven Van Caekenberghe