findTokens:escapedBy: (CSV parsing)
Last updated at 7:24 am UTC on 28 June 2016
From: Rudolf Rednose
Date: Sat, 18 Jun 2016 20:04:27 +0200
Subject: Aw: [Newbies] CSV file help
To: beginners@lists.squeakfoundation.org
Example how to parse CSV with the #findTokens:escapedBy: method in a Workspace.
|data fields|
data := OrderedCollection new.
data add: '02/04/2016 Thu,,"COSTCO WHSE #0388 000000000990388 - OAK BROOK, IL",,,,,37.00,,,,,,,,' copy.
data add: '02/05/2016 Fri,,"ELECTRONIC PAYMENT RECEIVED-THANK",,,,,-443.52,,,,,,,,' copy.
data add: '02/06/2016 Sat,,"COSTCO WHSE #1088 000000000991088 - BOLINGBROOK, IL",,,,,50.86,,,,,,,,' copy.
data do:[ :item |
fields := item findTokens:',' escapedBy: '"'.
"show result in Transcript"
Transcript cr; show: 'Line: ', item; cr; show: 'Number of Fields:', fields size asString; cr.
1 to: fields size do:[:n| Transcript show:( n asString,' [', (fields at: n),']');cr]
].
Transcript output
Line: 02/04/2016 Thu,,"COSTCO WHSE #0388 000000000990388 - OAK BROOK,
IL",,,,,37.00,,,,,,,,
Number of Fields:16
1 [02/04/2016 Thu]
2 []
3 [COSTCO WHSE #0388 000000000990388 - OAK BROOK, IL]
4 []
5 []
6 []
7 []
8 [37.00]
9 []
10 []
11 []
12 []
13 []
14 []
15 []
16 []
Line: 02/05/2016 Fri,,"ELECTRONIC PAYMENT RECEIVED-THANK",,,,,-443.52,,,,,,,,
Number of Fields:16
1 [02/05/2016 Fri]
2 []
3 [ELECTRONIC PAYMENT RECEIVED-THANK]
4 []
5 []
6 []
7 []
8 [-443.52]
9 []
10 []
11 []
12 []
13 []
14 []
15 []
16 []
Line: 02/06/2016 Sat,,"COSTCO WHSE #1088 000000000991088 -
BOLINGBROOK, IL",,,,,50.86,,,,,,,,
Number of Fields:16
1 [02/06/2016 Sat]
2 []
3 [COSTCO WHSE #1088 000000000991088 - BOLINGBROOK, IL]
4 []
5 []
6 []
7 []
8 [50.86]
9 []
10 []
11 []
12 []
13 []
14 []
15 []
16 []