trefan123 Posted July 23, 2018 Posted July 23, 2018 (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 July 23, 2018 by trefan123 Quote
Lee Mac Posted July 23, 2018 Posted July 23, 2018 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. Quote
BIGAL Posted July 24, 2018 Posted July 24, 2018 (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 July 25, 2018 by BIGAL correct use of LM:readcsv Quote
SLW210 Posted July 24, 2018 Posted July 24, 2018 Please read the Code Posting Guidelines and have your Code to be included in Code Tags.[NOPARSE] Your Code Here[/NOPARSE] = Your Code Here Quote
trefan123 Posted July 24, 2018 Author Posted July 24, 2018 thanks for the help, this beginer learnt something again Quote
Lee Mac Posted July 24, 2018 Posted July 24, 2018 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. Quote
BIGAL Posted July 25, 2018 Posted July 25, 2018 oops thanks Lee did not test so rewrote and tested updated code. Quote
trefan123 Posted July 27, 2018 Author Posted July 27, 2018 (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 Edited July 27, 2018 by trefan123 addition Quote
BIGAL Posted July 29, 2018 Posted July 29, 2018 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 ? Quote
trefan123 Posted July 30, 2018 Author Posted July 30, 2018 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 Quote
BIGAL Posted July 30, 2018 Posted July 30, 2018 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. Quote
trefan123 Posted July 30, 2018 Author Posted July 30, 2018 yip but have learnt quite a bit in the last month Quote
trefan123 Posted July 30, 2018 Author Posted July 30, 2018 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.:) Quote
BIGAL Posted July 31, 2018 Posted July 31, 2018 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 "") ) Quote
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.