Author Topic: Convert one csv file to another with lisp  (Read 1692 times)

0 Members and 1 Guest are viewing this topic.

PM

  • Guest
Convert one csv file to another with lisp
« on: April 01, 2020, 08:19:19 AM »
Hi. I have test.csv file  and i want to convert it to test2.csv. Is any way to do this with lisp. I try to do it in excell but i can not find a fast way to do this. I did it manualy. But when i have a lot of rows with data is difficult.

Thanks

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: Convert one csv file to another with lisp
« Reply #1 on: April 01, 2020, 09:19:53 AM »
Here you go...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:csv-pts-2-csv-pts+hor-proj ( / f1 fn1 l lst lstpairs p1 p2 p3 lstt f2 fn2 )
  2.   (setq f1 (getfiled "Select PTS CSV file..." "\\" "csv" 16))
  3.   (setq fn1 (open f1 "r"))
  4.   (while (setq l (read-line fn1))
  5.     (setq lst (cons l lst))
  6.   )
  7.   (setq lst (reverse lst))
  8.   (foreach l lst
  9.     (setq lst (subst (list (atof (substr l 1 (vl-string-search "," l))) (atof (substr l (+ 2 (vl-string-search "," l))))) l lst))
  10.   )
  11.   (setq lstpairs (mapcar '(lambda ( a b ) (list a b)) lst (cdr lst)))
  12.   (foreach pair lstpairs
  13.     (setq p1 (car pair) p3 (cadr pair))
  14.     (setq p2 (list (car p3) (cadr p1)))
  15.     (setq lstt (cons p1 lstt) lstt (cons p2 lstt))
  16.   )
  17.   (if (not (vl-position (last lst) lstt))
  18.     (setq lstt (cons (last lst) lstt))
  19.   )
  20.   (setq lstt (reverse lstt))
  21.   (setq f2 (getfiled "Select PTS with HOR-PROJ CSV file..." "\\" "csv" 1))
  22.   (setq fn2 (open f2 "w"))
  23.   (foreach pt lstt
  24.     (write-line (strcat (rtos (car pt)) "," (rtos (cadr pt))) fn2)
  25.   )
  26.   (close fn2)
  27.   (startapp "EXPLORER" f2)
  28.   (princ)
  29. )
  30.  

HTH., M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: Convert one csv file to another with lisp
« Reply #2 on: April 01, 2020, 09:44:35 AM »
Don't mind me; I had a quick moment and I wanted to try to write a routine to merge lists (what I thought is the core of the problem). This was me just playing with AutoLisp.

Code - Auto/Visual Lisp: [Select]
  1. (setq lst1 '(1 2 3 4)
  2.       lst2 '(5 6 7 8))
  3.  
  4. ( (lambda ( lst1 lst2 )
  5.     (apply 'append
  6.            (mapcar 'append
  7.                    (mapcar 'list lst1)
  8.                    (mapcar 'list lst2)))
  9.     )
  10.     lst1
  11.     lst2
  12.   )
> (1 5 2 6 3 7 4 8)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

PM

  • Guest
Re: Convert one csv file to another with lisp
« Reply #3 on: April 01, 2020, 01:55:12 PM »
Thank you ribarm

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Convert one csv file to another with lisp
« Reply #4 on: April 01, 2020, 02:42:17 PM »
Nice one John - I miss the old 'challenge' style academic threads which focussed more on fundamental programming technique and your post reminded me of them.  :-)

FWIW, you could omit one level of nesting from your code to achieve the same result, i.e.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (apply 'append (mapcar 'list lst1 lst2))
  2. (1 5 2 6 3 7 4 8)


JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: Convert one csv file to another with lisp
« Reply #5 on: April 01, 2020, 02:53:25 PM »
Nice one John - I miss the old 'challenge' style academic threads which focussed more on fundamental programming technique and your post reminded me of them.  :-)

FWIW, you could omit one level of nesting from your code to achieve the same result, i.e.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (apply 'append (mapcar 'list lst1 lst2))
  2. (1 5 2 6 3 7 4 8)

Yeah, they were a lot of fun and very, very helpful.

Ugh! That's what I was trying to do in the beginning (well, at least that is how I tried to "think out the method" like I used to be able to do). Thanks! :) ...I had a problem and it eventually morphed into that mess (my luck, I probably just misspelled "mapcar" and could have saved a few brain cells).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Convert one csv file to another with lisp
« Reply #6 on: April 01, 2020, 04:04:35 PM »
The transformation reminds me a lot of a matrix transposition, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq m '((0 1 2) (3 4 5) (6 7 8)))
  2. (
  3.     (0 1 2)
  4.     (3 4 5)
  5.     (6 7 8)
  6. )
  7. _$ (apply 'mapcar (cons 'list m))
  8. (
  9.     (0 3 6)
  10.     (1 4 7)
  11.     (2 5 8)
  12. )

Except that in this case, we are flattening the resulting list -
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq m '((1 2 3 4) (5 6 7 8)))
  2. (
  3.     (1 2 3 4)
  4.     (5 6 7 8)
  5. )
  6. _$ (apply 'mapcar (cons 'list m))
  7. (
  8.     (1 5)
  9.     (2 6)
  10.     (3 7)
  11.     (4 8)
  12. )
  13. (1 5 2 6 3 7 4 8)



JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: Convert one csv file to another with lisp
« Reply #7 on: April 01, 2020, 04:40:40 PM »
Continuing down the rabbit hole...

speaking of matrices, they remind me of safearrays.

Code - Auto/Visual Lisp: [Select]
  1. (vlax-safearray->list (vlax-make-safearray vlax-vbString '(0 . 2) '(0 . 2)))
>(("" "" "") ("" "" "") ("" "" ""))

And that, is the extent of my memory on that subject (that is all you guys get from me on sfearrays).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org