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

4

6

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.
Martita answered 12/3, 2012 at 14:3 Comment(0)
B
6

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: $,
Brower answered 12/3, 2012 at 16:31 Comment(2)
Thanks a whole bunch for the correction. Also, I don't have tokenBasedOn: as a message in my version of VisualWorks. Are there ways of importing new libraries into VisualWorks that I can take advantage of?Martita
That message has been in base VW at least as far as VW 7.0. Note that it's plural, tokens...BasedOn:Brower
A
3

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

Anomalistic answered 12/3, 2012 at 17:44 Comment(0)
M
2

Someone ported the NeoCSV parser from Pharo to VisualWorks. It might solve your problem.

Monde answered 24/7, 2012 at 10:16 Comment(0)
M
1

Probably the quickest way is loading the Parcel "GHCsvImportExport". Then you can do:

| reader lines |
reader := CsvReader onFileNamed: aFilename.
[lines := OrderedCollection new.
[reader atEnd] whileFalse:
     [lines add: reader nextLine.]] 
     ensure: [reader close].
lines inspect.
Melanosis answered 17/4, 2012 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.