joedevil Posted April 27, 2012 Posted April 27, 2012 Hi, i want to print the following lines in an opened text file: num01 num02 string03 -4.344 -3.842 string_val1 4.358 -3.820 string_val2 -0.130 -3.842 string_val3 i use (princ (...) file) and (princ "\t\t" file) two taps between every two strings. because of the different length of the data, the data are not aligned to the respective column, such that every column is aligned with "num01", "num02" or "string03". is there a way to go around with this? Thx in advance. Quote
fixo Posted April 27, 2012 Posted April 27, 2012 Try these functions: (defun wrs (file lst / filename ) (setq filename (open file "W")) (foreach x lst (write-line (lst2str x "\t") filename) ) (close filename) (princ) ) (defun lst2str (lst sep) (if (cadr lst) (strcat (vl-princ-to-string (car lst)) sep (lst2str (cdr lst) sep) ) (vl-princ-to-string (car lst)) ) ) (setq lst (list (list "num01" "num02" "string03") (list "-4.344" "-3.842" "string_val1" ) (list "4.358" "-3.820" "string_val2" ) (list "-0.130" "-3.842" "string_val3"))) ;;Usage: (wrs "C:\\Test\\ahhah.csv" lst);<--- change file name to suit ~'J'~ Quote
pBe Posted April 27, 2012 Posted April 27, 2012 (edited) [/b][/color] EDIT: My bad. i thought its the other way around [retarded ] Edited April 27, 2012 by pBe Quote
joedevil Posted April 27, 2012 Author Posted April 27, 2012 Try these functions: (defun wrs (file lst / filename ) (setq filename (open file "W")) (foreach x lst (write-line (lst2str x "\t") filename) ) (close filename) (princ) ) (defun lst2str (lst sep) (if (cadr lst) (strcat (vl-princ-to-string (car lst)) sep (lst2str (cdr lst) sep) ) (vl-princ-to-string (car lst)) ) ) (setq lst (list (list "num01" "num02" "string03") (list "-4.344" "-3.842" "string_val1" ) (list "4.358" "-3.820" "string_val2" ) (list "-0.130" "-3.842" "string_val3"))) ;;Usage: (wrs "C:\\Test\\ahhah.csv" lst);<--- change file name to suit ~'J'~ hi fixo, i tried the function but i stll got the same... Thx for the help any way... and this is wat i see in wordpad: num01 num02 string03 -4.344 -3.842 string_val1 4.358 -3.820 string_val2 -0.130 -3.842 string_val3 Quote
pBe Posted April 27, 2012 Posted April 27, 2012 Would you setlle for " " instead of tab? or you really want tabs? Do you have a routine for converting teh test string to a list? For space " " not tab "\t" and .txt format (defun _strnum (val tl / p) (repeat (- tl (strlen val)) (setq p (strcat " " (if p p " ")))) (strcat val p)) List after parsing (setq lst '(("num01" "num02" "string03")("-4.344" "-3.842" "string_val1") ("4.358" "-3.820" "string_val2" )("-0.130" "-3.842" "string_val3"))) The code (foreach itm lst (write-line (strcat (_strnum (car itm) 12)(_strnum (cadr itm) 12) (last itm)) filename) ) It would look like this num01 num02 string03 -4.344 -3.842 string_val1 4.358 -3.820 string_val2 -0.130 -3.842 string_val3 We can also make one for tabs if thats what you require. Quote
pBe Posted April 27, 2012 Posted April 27, 2012 (edited) For Tabs: (setq _HiLo (lambda (lst mode p) (+ (apply mode (mapcar '(lambda (j) (strlen (p j))) lst)) 2) ) ) (setq fval (_HiLo lst 'Max car) Sval (_HiLo lst 'Max cadr)) (foreach itm lst (write-line (strcat (_strnum (car itm) fval)"\t"(_strnum (cadr itm) Sval) "\t" (last itm)) filename) ) HTH Edited April 27, 2012 by pBe Quote
joedevil Posted April 30, 2012 Author Posted April 30, 2012 hi pBe, i think i would settle for the space one... "" it works finely... thank u very much for the help Quote
pBe Posted April 30, 2012 Posted April 30, 2012 hi pBe, i think i would settle for the space one... ""it works finely... thank u very much for the help You're Welcome Joedevil. Glad to help You can adjust the number of space " " here --> (_strnum (car itm) 12), that is total lenght of string per column except the last. as for importing it back to cad, that will require another routine to consider those whitespaces , but its fairly easy. Holler if you need a code for that too. Cheers Quote
MSasu Posted April 30, 2012 Posted April 30, 2012 Joedevil, it will be interesting also to clarify which are your goals with this file and his aligned data. If is about a report that is to be printed, then pBe’s solution with spaces is the most suitable since using may conduct to misalignments in some cases. If the file is a database that would be accessed later to read his content, a one character separator (, "," or ";") is a much effective solution. 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.