Jump to content

Recommended Posts

Posted

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.

Posted

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'~

Posted (edited)
[/b][/color]

 

EDIT: :shock: My bad. i thought its the other way around [retarded ]

Edited by pBe
Posted
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

Posted

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.

Posted (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 by pBe
Posted

hi pBe, i think i would settle for the space one... ""

it works finely... thank u very much for the help

Posted
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 :)

Posted

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.

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