Ricky.Calle Posted July 2, 2019 Share Posted July 2, 2019 Hello, I would like to get some help with the task I have in hand. let me explain my dilemma. I have a "CVS" file that is comma delimited format. the file has two pieces of information separated by comma. Example bellow: 50,WTF-00011 75,WFT-00023 100,WFT-00050 etc.... the file could have up 6000 lines. I don't do not know how to write a code separate the line using the comma as a separator. I would like to assign the values before the comma into a variable "VAR1" for later use later in the program. I would like to assign the values after the comma into a different variable "VAR2" for use later in the program. Thank you in advance for your help. Kind regards, Ricky Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 2, 2019 Share Posted July 2, 2019 You may use my existing Read CSV function for this task - I have included a test program on the linked page to demonstrate how to call the function in a program. You may then iterate over the list of rows and perform the necessary operations on each pair of items. Quote Link to comment Share on other sites More sharing options...
asos2000 Posted July 7, 2019 Share Posted July 7, 2019 (edited) The Simple way Thanks LEE for the subroutine (defun c:test ( / data file ) (if (and (setq file (getfiled "Select CSV File" "" "csv" 16)) (setq data (LM:readcsv file)) (setq e -1) ) (repeat (length data) (setq var (nth (setq e (1+ e)) data)) (setq var1 (nth 0 var )) (setq var2 (nth 1 var )) ; The Code ) ) (princ) ) (princ) ;; Read CSV - Lee Mac ;; Parses a CSV file into a matrix list of cell values. ;; csv - [str] filename of CSV file to read (defun LM:readcsv ( csv / des lst sep str ) (if (setq des (open csv "r")) (progn (setq sep (cond ((vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList")) (","))) (while (setq str (read-line des)) (setq lst (cons (LM:csv->lst str sep 0) lst)) ) (close des) ) ) (reverse lst) ) ;; CSV -> List - Lee Mac ;; Parses a line from a CSV file into a list of cell values. ;; str - [str] string read from CSV file ;; sep - [str] CSV separator token ;; pos - [int] initial position index (always zero) (defun LM:csv->lst ( str sep pos / s ) (cond ( (not (setq pos (vl-string-search sep str pos))) (if (wcmatch str "\"*\"") (list (LM:csv-replacequotes (substr str 2 (- (strlen str) 2)))) (list str) ) ) ( (or (wcmatch (setq s (substr str 1 pos)) "\"*[~\"]") (and (wcmatch s "~*[~\"]*") (= 1 (logand 1 pos))) ) (LM:csv->lst str sep (+ pos 2)) ) ( (wcmatch s "\"*\"") (cons (LM:csv-replacequotes (substr str 2 (- pos 2))) (LM:csv->lst (substr str (+ pos 2)) sep 0) ) ) ( (cons s (LM:csv->lst (substr str (+ pos 2)) sep 0))) ) ) (defun LM:csv-replacequotes ( str / pos ) (setq pos 0) (while (setq pos (vl-string-search "\"\"" str pos)) (setq str (vl-string-subst "\"" "\"\"" str pos) pos (1+ pos) ) ) str ) Edited July 7, 2019 by asos2000 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.