Get list from CSV line

Get list from CSV line

Browning_Zed
Advocate Advocate
3,628 Views
25 Replies
Message 1 of 26

Get list from CSV line

Browning_Zed
Advocate
Advocate

Hi All,

I need to find a value in a line of a CSV file, and get a list from a line. The CSV file is located in Trusted paths and Support paths.

In the attached file, column A contains the names with which the search will be carried out.

Thus, the CSV file is found first, and then the line in the CSV file is found. And, if there is * character in the cell before the data, then a real number is returned, and if there is no * character, then a string value is returned.

For instance:

  1. if "text-arial" is found, then it will return the list '("Arial" "arial.ttf" 1 0)
  2. if "text-simplex" is found, then it will return the list '("Simplex" "simplex.shx" 0.8 0.279253)
  3. if "text-tnr" is found, then it will return a list '("TNR" "times.ttf" 1 0)

Can anyone help me?

0 Likes
Accepted solutions (2)
3,629 Views
25 Replies
Replies (25)
Message 2 of 26

hak_vz
Advisor
Advisor

 

(defun string_to_list ( str del / pos )
        (if (setq pos (vl-string-search del str))
            (cons (substr str 1 pos) (string_to_list (substr str (+ pos 1 (strlen del))) del))
            (list str)
        )
    )  

(defun csc_in (del / file1 f a)
	(setq f (getfiled "Open File" (getvar "dwgprefix") "xyz" 2))
	(setq file1 (open f "r") ret (list))
	(while (setq a (read-line f))
		(setq ret (append ret (list (string_to_list a del))))
		
	)
	(close file1)
	ret
)

;usage  (csv_in ",")
; list is stored in variable global variable ret for further use

 

 

Try to use this.

In function csv_in "," is delimiter change it according what you use. You can open csv file in notepad to see what is your delimiter.

 

If you know path to the csv file you can remove function "getfiled" and in function "open" replace f with file path

i.e. "D:\\folder\\filename.csv"

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 3 of 26

Browning_Zed
Advocate
Advocate

This is not what I need, maybe I told you wrong.
I will try to explain step by step:
1. I need a function something like
(defun RCV (cellname /)
(progn ...)
)
2. The function is run where the argument includes the name of the required cell:
(RCV "text-arial")
The function checks the values in the specified CSV file until it finds a cell with the content "text-arial".
3. After that, the function returns a list from the CSV string containing "text-arial".
Line in CSV file = "text-arial, Arial, arial.ttf, 1,0"
Should return list = '("Arial" "arial.ttf" 1 0)

0 Likes
Message 4 of 26

pbejse
Mentor
Mentor

@Browning_Zed wrote:

1. I need a function something like
(defun RCV (cellname /)
(progn ...)


The program still needs to read the file @Browning_Zed , either using the method ReadAll method or line by line.

And then check the collected list with the cellname argument

 

 

(defun _DelTolst ( str m / pos x lst lst2)
	(if (setq pos (vl-string-position m str))
	   (cons (substr str 1 pos) (_DelTolst (substr str (+ pos 2)) m ))
	   (list str)
	   )
)
(defun RCV (cellname / contentList theCSV opf a f)
  (if (setq contentList	nil		; should be declared local | For testing purposes
	    theCSV	(findfile "foundfirst.csv");<-- the file on SFSP
      )
    (progn
      (setq opf (open theCSV "r"))
      (While (setq a (read-line opf))
	(Setq contentList (cons (_DelTolst a 44) contentList))
      )
      (close opf)
      (if (Setq f (Cdr (assoc cellname contentList)))
	(list (Car f)
	      (cadr f)
	      (distof(substr (caddr f) 2))
	      (distof(substr (cadddr f) 2))
	)
      )
    )
  )
)

 

if you  remove the "*" character on the  D & E, then this line

 

(list (Car f)
	      (cadr f)
	      (distof (substr (caddr f) 2))
	      (distof (substr (cadddr f) 2))
	)

 

could be

(list (Car f)
	      (cadr f)
	      (distof (caddr f))  (distof (cadddr f))
	)

HTH

0 Likes
Message 5 of 26

Browning_Zed
Advocate
Advocate

Not sure why, but the function being executed (RCV "text-arial") returns nil.

0 Likes
Message 6 of 26

Browning_Zed
Advocate
Advocate

The foundfirst.csv file is is located in Trusted paths and Support paths, but the function returns nil.

0 Likes
Message 7 of 26

hak_vz
Advisor
Advisor

@Browning_Zed 

Since you are learning autolisp it would be good to try yourself to rewrite provided code

If you need to run code line bi line than it's something like

 

 

 

 

(defun string_to_list ( str del / pos )
        (if (setq pos (vl-string-search del str))
            (cons (substr str 1 pos) (string_to_list (substr str (+ pos 1 (strlen del))) del))
            (list str)
        )
    )  

(defun csc_in ( / file1 f a)
	(setq file1 (open "D:\\myfile.csv" "r"))
	(while (setq a (read-line f))
		(setq mylist (string_to_list a ",")))))
                ;reads a line from file
                ;let say ("text-arial" Arial, "arial.ttf" "1" "0")
		;your code here
         (if (= (car mylist) "text-arial"))
           ;do something
           )
      ;.......
	)
	(close file1)
)

 

Now since @pbejse  is also providing code, decide which one you'll use and stick with it.

I won't post further so  not to confuse you (only if you insist).

What I'd like to see is your code.

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 8 of 26

pbejse
Mentor
Mentor

@Browning_Zed wrote:

Not sure why, but the function being executed (RCV "text-arial") returns nil.


There a couple of reasons for that,

First:

(findfile "ThisShouldbeTheYourCSVfilename.csv")

Another [ if the first line in not nil ]

;; the cellname value is nowhere on the list ;;
(Setq f (Cdr (assoc cellname contentList)))

 

0 Likes
Message 9 of 26

pbejse
Mentor
Mentor

@Browning_Zed wrote:

The foundfirst.csv file is is located in Trusted paths and Support paths, but the function returns nil.


There's your answer. IF the file [ correct filename ] is indeed within SFSP then it should not return nil.

tell you what, paste this on command line

(findfile "foundfirst.csv");<--- make sure its the correct name OK?

 

0 Likes
Message 10 of 26

Browning_Zed
Advocate
Advocate

(findfile "foundfirst.csv")

return "F:\\foundfirst.csv"

 

(Setq f (Cdr (assoc cellname contentList)))

return nil.

 

Just in case, I will attach a csv file. It contains the cell to search for:
(RCV "text-arial")

0 Likes
Message 11 of 26

Browning_Zed
Advocate
Advocate

Unfortunately, my knowledge of LISP programming is not yet sufficient to edit the source code.

0 Likes
Message 12 of 26

pbejse
Mentor
Mentor
Accepted solution

@Browning_Zed wrote:

(findfile "foundfirst.csv")

return "F:\\foundfirst.csv"

 

(Setq f (Cdr (assoc cellname contentList)))

return nil.

 

Just in case, I will attach a csv file. It contains the cell to search for:
(RCV "text-arial")


Here's the thing. 

The code is looking for a comma delimited file, what you have is ";"

 

changing this 
(Setq contentList (cons (_DelTolst a 44) contentList))
to
(Setq contentList (cons (_DelTolst a 59) contentList))
Should work

 

The CSV file is save under UTF-8 Unicode format

I saved the file to ANSI format

And are you keeping the "*" character on columns D & E? 

If the answer is yes.

 

(defun RCV (cellname / contentList theCSV opf a f)
  (if (setq contentList	nil		; should be declared local | For testing purposes
	    theCSV	(findfile "foundfirst.csv")
      )
    (progn
      (setq opf (open theCSV "r"))
      (While (setq a (read-line opf))
	(Setq contentList (cons (_DelTolst a 59) contentList))
      )
      (close opf)

      (if (Setq f (Cdr (assoc cellname contentList)))
	(list (Car f)
	      (cadr f)
	      (distof (substr (caddr f) 2))
	      (distof (substr (cadddr f) 2))
	)
      )
    )
  )
)

 

if not  use this bit as the list format

 

(list (Car f)
	      (cadr f)
	      (distof (caddr f))  (distof (cadddr f))
	)

 

HTH

 

Refer to attached ANSI formatted foundfirst.csv

 

Message 13 of 26

hak_vz
Advisor
Advisor
Give it a try, you may surprise yourself.
I think that our goal here is not to constantly provide finished complex code,
but to inspire new members to learn writing code.
This was an easy task to start with. If you don't step inn you'll newer learn. Writing code is repetitive process of trial and error.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 14 of 26

Browning_Zed
Advocate
Advocate

One more question. The name of the desired cell specified in the function must exactly match the content of the CSV cell. How can the content of a CSV cell string be normalized so that spaces and other case are not taken into account if present in the cell string?

0 Likes
Message 15 of 26

pbejse
Mentor
Mentor

@Browning_Zed wrote:

One more question. The name of the desired cell specified in the function must exactly match the content of the CSV cell. How can the content of a CSV cell string be normalized so that spaces and other case are not taken into account if present in the cell string?


 

You mean for the first column cells or all cell?

additional strings or characters on the cell? case senstive?

post an example please.

 

 

0 Likes
Message 16 of 26

Browning_Zed
Advocate
Advocate

I mean the cells of the first column.
For example, the cell content of the first column
"txt -TNR"
I indicate in the function that I need to extract a cell
"txt-tnr"
and nil is returned. How can I avoid this error?

It is necessary to normalize spaces and case.

0 Likes
Message 17 of 26

pbejse
Mentor
Mentor

@Browning_Zed wrote:

I mean the cells of the first column.
"txt -TNR"

It is necessary to normalize spaces and case of the string.


Forgive me for saying this, is it not easier to fix the first column of the csv than to create a catch-all program to deal with invalid entries? its just simple common sense on the side of the user. 

 

I understand if there are multiple copies of the CSV, but if its just one source, then it does not make sense at all.

I'm thinking ther's more to it than what you are telling me or  just spaces.

 

Is the CSV created by a program that reads the a file and export the style list?

 

 

0 Likes
Message 18 of 26

Browning_Zed
Advocate
Advocate

CSV file already exists (not created by the program). The fact is that a cell may contain a string that contains a space, and this may not be detected immediately. And as a result, routine will not work correctly.

0 Likes
Message 19 of 26

pbejse
Mentor
Mentor

Dont know what else to tell you. I find it hard to believe that not any one of the users cant even check the csv file at least once for invalid entries. 

 

It's an easy fix anyway, but before we proceed, let me go back to my previous query,  Why the need for "*" on columns D & E?

 

0 Likes
Message 20 of 26

Browning_Zed
Advocate
Advocate

As it turned out later, the * symbol is not needed. At first I thought about how to make a distinction between a string and a real number dataset. But for this it was enough to use the functions "atof" and "atoi".

0 Likes