Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to, in smalltalk, read and process contents of CSV file

I am attempting to read and process the contents of a csv file in smalltalk(visualworks), but I seem to be having some hard time getting the string to split into an array please. Below is code I have been able to get working. What I am missing is the piece that splits the content of myLine variable, which is a comma-delimited string, into an array of strings, to be added to a TwoDList. Please help with any information you may have on how to approach this please. Thanks

SpreadsheetReadCSV:  inFilename
    |inStream myLine rcnt|  
      rcnt := 0.
       " scan thru the text lines, using a traditional i/o loop "
       inStream :=  (inFilename asFilename) readStream  .
       [ inStream atEnd ] whileFalse: [
             rcnt := rcnt + 1. 
            myLine := inStream upTo: Character cr.
                "Process the commadelimited string here"
       ].
      inStream inspect. 
      inStream close.
   ^myLine.
like image 370
Kobojunkie Avatar asked Mar 12 '12 14:03

Kobojunkie


2 Answers

1) You can turn a string into a stream as well, so you can use the same technique you used to parse the file into lines:

myLine := (inStream upTo: Character cr) readStream.
[ myLine atEnd ] whileFalse: [ | myCell |
  myCell := myLine upTo: $,.
  "Do whatever with the cell" ]

2) You can split a string into pieces using tokensBasedOn:

myLine tokensBasedOn: $,
like image 146
Martin Kobetic Avatar answered Sep 21 '22 22:09

Martin Kobetic


You might want to check out the CSVParser project on squeaksource. It should not be hard to make it work in Visualworks.

This will give you support for all csv files (e.g with escaped chars, quoted fields,etc)

Also see this post

like image 44
Johan B Avatar answered Sep 21 '22 22:09

Johan B