Page 1 of 1 1
Topic Options
#121454 - 2004-06-17 05:31 PM how do i read in and parse csv file
kurty Offline
Lurker

Registered: 2004-06-17
Posts: 2
hi, perhaps this is a silly question but kix scripting is new to me

how do i read in a csv file and extract each value on every line as a variable

after each line is parsed a command would run using the variables extracted

the csv file could have any number of lines and approx 10 values, comma seperated per line - so the script runs until the eof is reached

thanks for any assistance

Top
#121455 - 2004-06-17 05:39 PM Re: how do i read in and parse csv file
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
here's a quick script that will read lines in $filename and split each comma-delimited $field and display it ... doesn't account for quoted strings etc.

Code:

break on

$filename = "t.csv"

if open(1,$filename) = 0

$line = readline(1)

while @error = 0

$array = split($line,",")

?

for each $field in $array
? $field
next

$line = readline(1)

loop

$= close(1)

endif

exit 1



-Shawn

Top
#121456 - 2004-06-17 07:29 PM Re: how do i read in and parse csv file
kurty Offline
Lurker

Registered: 2004-06-17
Posts: 2
cool, thats great thanks..but how do i assign an incremental variable counter to each values i read per line

i thought i could put a counter in and assign each field value to the incremting var counter but it just returns 1 2 3 4 5 6 7 not the field values!


if open(1,$filename) = 0
$line = readline(1)
while @error = 0
$c=1
$array = split($line,",")
?
for each $field in $array
$var($c) = $field
? "field value is : " $field
? "variable value counter :" $c " holds value : " $var($c)
$c=$c+1
next
$line = readline(1)
loop
$= close(1)
endif


any assitance or pointers would be appreciated

Top
#121457 - 2004-06-17 08:38 PM Re: how do i read in and parse csv file
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
You can just address the elements right out of the array itself, instead of enumerating it like a collection, for example - if you were going to pass the fields to a SHELL command line (just simulated here):

Code:

break on

$filename = "t.csv"

if open(1,$filename) = 0

$line = readline(1)

while @error = 0

$c=1

$array = split($line,",")

?"cmd /c " + $array[0] + " /switch1:" + $array[1] + " /switch2:" + $array[2]

$line = readline(1)

loop

$= close(1)

endif



-Shawn

Top
#121458 - 2004-06-17 09:24 PM Re: how do i read in and parse csv file
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
I believe that you can do the same thing with READEXCEL() and/or READEXCEL2() UDFs as they will load in the CSV into Excel and you can read the elements..

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#121459 - 2004-06-18 02:36 AM Re: how do i read in and parse csv file
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
WARNING! It is possible to have a comma in a CSV file that is not a dilimiter (hint, they are wrapped in quotes). Example...

"lastname, first mi"

Top
#121460 - 2004-06-18 09:58 AM Re: how do i read in and parse csv file
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
There is an "MS CSV" safe udf I posted in this thread:
http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Board=UBB1&Number=35833

IIRC it handles the '"' and ',' weirdness.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 871 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.056 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org