PDA

View Full Version : help on read and write to .csv files


jitesh789
2007-10-20, 09:45 PM
Hi
Can any one please help me on read and write commands in lisp how to use it and how does read command read from a file for eg i want to plot a traverse in cad my csv will have
point, easting,northing,rl,code. how to read this and how to decide on decimal places
Also if i have a polygon how can i select the line and get all the coordinates of the end point

Opie
2007-10-21, 04:09 AM
You, of course, need to open the file for reading and assign this file pointer to a variable. You can then read-line one line from the file pointer at a time. You could then loop through this string while looking for the commas, which delimit your fields (point, easting, northing, etc.).

Of course, if you wanted to write-line this information to an opened file for write.

jitesh789
2007-10-21, 06:26 AM
Thanks but i need to know that when i read a file what does it takes first suppose i have 5 columns in csv format i.e 5 values separated by commas and i have 10 rows do i have to define column width or use car functions if so please tell me how to.

mweaver
2007-10-22, 01:55 PM
This is quick and dirty, no testing, no error checking:

(defun ReadCSVFile(FileName / rtlist)
(setq fileh (open FileName "r"))
(while (setq line (read-line fileh))
(setq rtlist (cons (str2list line ",") rtlist))
)
(reverse rtlist)
)

;;;By John Uhden, as posted to the autodesk customization newsgroup 06 Feb 2003
(defun Str2List (str pat / i j n lst)
(cond
((/= (type str)(type pat) 'STR))
((= str pat)'(""))
(T
(setq i 0 n (strlen pat))
(while (setq j (vl-string-search pat str i))
(setq lst (cons (substr str (1+ i)(- j i)) lst)
i (+ j n)
)
)
(reverse (cons (substr str (1+ i)) lst))
)
)
)

Note: This fails on values with a quoted comma

Here is a thread on TheSwamp.org with code to deal with quoted commas: http://www.theswamp.org/index.php?topic=19527.0