Jump to content

Recommended Posts

Posted (edited)

Good day to all,

I understand to read a csv line for tables we can use

 

(setq csvline (read-line f))

 

This starts from first row

 

I am a bit lost / cant find how to read a specific row, say row 5 only

Also is it possible to stack the output.

 

(defun c:csvin ()

(setq f (open "D:/03-Scans/test.csv" "r")) ;Open csv file

(setq csvdata (read-line f)) ;reads first line

(command "text" (getpoint) "" "" csvdata) ;Pick place to insert

)

 

Regards

Trefan123

Edited by trefan123
Posted

Either something like:

(repeat 5 (setq csvline (read-line f)))

Or, using my Read CSV function:

(setq csvline (nth 4 (LM:readcsv "YourFilename.csv")))

Note that the latter will return a list of cell values, whereas the former will return a string.

Posted (edited)

Lee with great answers as usual especially LM:Readcsv.

 

(defun c:csvin ( / f x csvdata)
(setq f (open "D:/03-Scans/test.csv" "r")) ;Open csv file
(setq x (getint "Enter line number to read"))
(repeat x (setq csvline (read-line f)))
(command "text" (getpoint) "" "" csvdata) ;Pick place to insert
(close F) ; good idea to close file
)

 

(defun c:csvin ( / f x csvdata)
(while (setq x (getint "Enter line number to read"))
(setq f (open "D:/03-Scans/test.csv" "r")) ;Open csv file
(repeat x (setq csvline (read-line f)))
(command "text" (getpoint) "" "" csvdata) ;Pick place to insert
(close F) ; good idea to close file
)
)

Better using readcsv

(defun c:csvinm ( / f x csvlines csvdata)
(setq fname (getfiled "Select a csv File" "D:/03-Scans/" "csv" 4))
(while (setq x (getint "Enter line number to read"))
(setq f (open fname "r"))
(repeat  x
(setq csvlines  (LM:csv->lst (read-line F) "," 1))
)
(close F)
(setq y 0)
(setq textans "")
(repeat (length csvlines)
(setq textans (strcat (nth y csvlines) " " textans))
(setq y (+ y 1))
)
(command "text" (getpoint) "" textans)
)
(princ)
)
(c:csvinm)

Edited by BIGAL
correct use of LM:readcsv
Posted

thanks for the help, this beginer learnt something again

Posted
Better using readcsv

(defun c:csvinm ( / f x csvlines csvdata)

(setq f (open "D:/03-Scans/test.csv" "r")) ;Open csv file
(setq csvlines  (LM:readcsv F)))
(close F) ; good idea to close file
)

(while (setq x (getint "Enter line number to read"))
(setq csvdata (nth (- x 1) csvlines)) 
(command "text" (getpoint) "" "" csvdata) ;Pick place to insert
)
(princ)
)

 

BIGAL, Note that my LM:readcsv function accepts a filename (string) argument, not a file descriptor.

Posted

oops thanks Lee did not test so rewrote and tested updated code.

Posted (edited)

Sorry still messing / trying to understand

(defun c:csvin ( iSR / f x csvdata)
(while (setq iSR (getint "Rating :- "))
(if (= iSR 150) ; where Rating is 150#
(progn
(setq f (open "D:/03-Scans/150.csv" "r")) ;Open 150 csv file

 

So if I need it to open a specific file can I do this?

Then try and get the next input to set the row to read

:huh:

Edited by trefan123
addition
Posted

I rewrote the code posted above to include a pick a CSV. Are you now saying you want to look at each row and decide wether to add to a table ?

Posted

Please bear with me as I dont fully understand and usually just hash lisps together.

So I have a new line of attack.

Its to use a DCL file (care of Terry Miller's site) and slowly taken out what i can see might not be needed, plus added.

So the lisp will ask for the Rating and item

"Rating" will be the File

"Item" being the line within the file that needs to be placed in the drawing

 

Maybe i should use an excel sheet but the lisps i have seen i cant make head or tail whats going on, mainly due to them written in v.basic.

So its the csv file or notepad for my understanding

So sorry for the mash up off all

 

Hope this explains it as clear as mud

iStress.DCL

IStress.LSP

Posted

VL-string-search will return a string in a read-line from either a csv or just a plain txt file. So you would just read-line till a match is found. It sounds to me you are making it to big a task.

Posted

yip but have learnt quite a bit in the last month

Posted

okay got the code sorted i think, well seems to work.

(cond
	((= Value2$ "FLG-WN")
(setq f (open "D:/03-Scans/iStressFLG-WN.csv" "r"))
(repeat (atoi Value1$)  (setq csvdata (read-line f)))
(command "text" (getpoint) "" "" csvdata)
)

 

now can one change the row info "130,6.4kg," from the csv to be in column form, without the commas so the 130 would be on top 6.4kg below.

or would the best be to opt and go via the excel route as i have seen on other posts.:):)

Posted

Try this must have Lee-mac CSV -> list

 

 

; remove (command "text" (getpoint) "" "" csvdata)

(setq lstcsv (LM:csv->lst csvdata))
(setq x 0)
(setq pt1 (getpoint "Pick 1st point for mtext"))
(setq pt2 (getpoint "Pick 2nd point for mtext"))
(command "-mtext" )
(while (= (getvar "cmdactive") 1 )
(command pt1 pt2)
(repeat (length lstcsv)
(command (nth x lstcsv))
(setq x (+ x 1))
)
(command "")
)

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...