Get list from CSV line

Get list from CSV line

Browning_Zed
Advocate Advocate
3,635 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,636 Views
25 Replies
Replies (25)
Message 21 of 26

pbejse
Mentor
Mentor
(defun RCV (cellname / RemChar contentList theCSV opf a f)
(defun RemChar (str c / str)
	   (while (vl-string-search c str)
	     (setq str (vl-string-subst "" c str))
	   )
	 )
  (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 (or (Setq f (Cdr (assoc cellname contentList)))
	      (setq f (cdr (vl-some '(lambda (s)
			  (if (vl-string-search (RemChar (strcase (car s)) " ")
				       (strcase cellname))
			    s))  contentList))
			    )
		)
	(list (Car f)
	      (cadr f) (atof (caddr f)) (atof (cadddr f))
	)
      )
    )
  )
)

 

0 Likes
Message 22 of 26

Browning_Zed
Advocate
Advocate

This version of the code works, but it still does not ignore the extra characters in the first cell, which I named "findCell". If it encounters a space in "findCell", it returns nil. I'll give the code in full:

 

 

(defun TG:txt-st ( findCell / RemChar contentList theCSV opf a *sl* NMS FONT WIDTH OBLIQUE )

(defun RemChar (str c / str)
	   (while (vl-string-search c str)
	     (setq str (vl-string-subst "" c str))
	   )
	 )

	(if (setq 
			contentList	nil
			theCSV	(findfile *tg_styles*)
		)
		(progn
			(setq opf (open theCSV "r"))
			(while
				(setq a (read-line opf))
				(setq contentList (cons (_DelTolst a 59) contentList))
			)
			(close opf)
      (if (or (Setq *sl* (Cdr (assoc findCell contentList)))
	      (setq *sl* (cdr (vl-some '(lambda (s)
			  (if (vl-string-search (RemChar (strcase (car s)) " ")
				       (strcase findCell))
			    s))  contentList))
			    )
		)
				(progn 
					(setq 
						NMS (car *sl*)
						FONT (cadr *sl*)
						WIDTH (atof (caddr *sl*))
						OBLIQUE (atof (cadddr *sl*))
					)
					(if (not (tblsearch "STYLE" NMS))
						(entmake 
							(list	'(0 . "STYLE")
									'(100 . "AcDbSymbolTableRecord")
									'(100 . "AcDbTextStyleTableRecord")
									(cons 2 NMS)
									(cons 3 FONT)
									(cons 41 WIDTH)
									(cons 50 OBLIQUE)
									'(40 . 0.0)
									'(70 . 0)
									'(71 . 0)
							)
						)
					)
				)
			)
		)
	)	(princ)
)

 

 

0 Likes
Message 23 of 26

pbejse
Mentor
Mentor
Accepted solution

@Browning_Zed wrote:

This version of the code works, but it still does not ignore the extra characters in the first cell, which I named "findCell". If it encounters a space in "findCell", it returns nil. I'll give the code in full:

 

 You mean the other way around? findcell variable has a space?  and by characters you mean not just space ( " " ) ?

findcell is the argument for the function TG:txt-st and NOT the value of the first cell from the CSV.

 

(TG:txt-st "txt -TNR")

 

the scenario you posted before is the cell column has the space

 


@Browning_Zed wrote:

. 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?

...

For example, the cell content of the first column
"txt -TNR"


To which the program kills ALL spaces ONLY on the first column  That is why its important to be clear. 

Are there anymore surprises? like not just " " character but something else 😊

 

BTW: i added str at the end of the sub RemChar, that could've been a factor as well (my bad )

 

Refer to attachment: TG_txt-st.lsp

 

I'm sure you know what to do in case there's no need for killing the findcell variable. or even there are more changes on the code.

 

Happy coding.

 

Message 24 of 26

Browning_Zed
Advocate
Advocate

I meant that when I call the function

 

 

(TG: txt-st "txt-arial")

 

 

 I meant that when I call the function
(TG: txt-st "txt-arial")
and if the cell "findCell" contains "txt- arial " the function will fail.
In the CSV I attached, you can see this miss.

0 Likes
Message 25 of 26

pbejse
Mentor
Mentor

@Browning_Zed wrote:

and if the cell "findCell" contains "txt- arial " the function will fail.


 

Not from where i'm sitting@Browning_Zed , working just fine and dandy.

 

_$ (foreach itm contentList (print itm)(princ))
("txt- arial " "Arial" "arial.ttf" "1" "0") 
("txt-simplex  _oblique" "Simplex íàêëîííûé" "simplex.shx" "0.8" "0.279253") 
("" "" "" "" "") 
("findCell" "NMS" "FONT" "WIDTH" "OBLIQUE") 
_$ (TG:TXT-ST "txt-arial");<--- call to sub
("Arial" "arial.ttf" "1" "0") ;<--- result
_$ 

Make sure you use the code attached on my previous post. Also consider removing this line on that code

(setq findCell(RemChar (strcase findCell) " "));<-- kill the space character on findcell variable

 

Because of the way you describe the problem, we ended up with too many versions of the code. and this forum wont allow us to edit previous postings. I'm throwing in the towel on this one.

 

Guess you need to find the bug on your own.

 

Message 26 of 26

Browning_Zed
Advocate
Advocate

Thank, I tried running your code again and everything works fine. Thank you so much! You are my hero, man!😀

0 Likes