TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: dubb on November 04, 2019, 12:36:20 PM

Title: How save a file as unicode
Post by: dubb on November 04, 2019, 12:36:20 PM
When creating a tab delimited text file I
Code: [Select]
(princ "A\tB\tC\n1\t2\t3 as a .csv file. However when I open it up in excel it doesn't recognize it. So then I found out how to save the file as unicode using notepad. After doing so, it open up correctly in excel.

How do I saveas or make the file a unicode type file in autolisp without actually writing unicode characters?
Title: Re: How save a file as unicode
Post by: Vaidas on November 04, 2019, 01:30:19 PM
Try to convert using iconv https://en.wikipedia.org/wiki/Iconv
Title: Re: How save a file as unicode
Post by: Lee Mac on November 04, 2019, 01:34:15 PM
I'm not sure that this will solve the problem of Excel recognising the file, but nevertheless this thread (https://www.theswamp.org/index.php?topic=39617.0) demonstrates how to read/write Unicode files.
Title: Re: How save a file as unicode
Post by: dubb on November 04, 2019, 06:49:31 PM
You know. I Googled it and led to a similar post. It was a matter of figuring out  how it was used. But this worked.  Thanks for sharing.

I'm not sure that this will solve the problem of Excel recognising the file, but nevertheless this thread (https://www.theswamp.org/index.php?topic=39617.0) demonstrates how to read/write Unicode files.
Title: Re: How save a file as unicode
Post by: BIGAL on November 04, 2019, 07:34:36 PM
Have you tried this way using the chr method.

Code: [Select]
(setq fo (open (setq fname "d:\\acadtemp\\testab.txt") "w"))
(write-line (strcat "a" (chr 9) "b" (Chr 9) "c") fo)
(write-line (strcat "a" (chr 9) (chr 9) "b" (Chr 9) (chr 9) "c") fo)
(close fo)

a   b   c
a      b      c

Code: [Select]
(write-line (strcat "f" (chr 9) (chr 9) "g" (Chr 9) (chr 9) "h" (chr 13) "d" (chr 9) (chr 9) "e") fo)
a   b   c
a      b      c
f      g      h
d      e

Title: Re: How save a file as unicode
Post by: dubb on November 05, 2019, 11:21:22 AM
I haven't. But it looks way complicated. The actual string I'm writing contains a lot of text. Looks like I would have to convert each letter using this method. That's interesting though, I might have use for it.
Have you tried this way using the chr method.

Code: [Select]
(setq fo (open (setq fname "d:\\acadtemp\\testab.txt") "w"))
(write-line (strcat "a" (chr 9) "b" (Chr 9) "c") fo)
(write-line (strcat "a" (chr 9) (chr 9) "b" (Chr 9) (chr 9) "c") fo)
(close fo)

a   b   c
a      b      c

Code: [Select]
(write-line (strcat "f" (chr 9) (chr 9) "g" (Chr 9) (chr 9) "h" (chr 13) "d" (chr 9) (chr 9) "e") fo)
a   b   c
a      b      c
f      g      h
d      e
Title: Re: How save a file as unicode
Post by: 57gmc on November 05, 2019, 03:39:31 PM
Looks like I would have to convert each letter using this method.
Al isn't converting any text. 9 is the ascii code for tab. He's just using (chr 9) instead of "\t".
Title: Re: How save a file as unicode
Post by: Lee Mac on November 05, 2019, 03:59:42 PM
Looks like I would have to convert each letter using this method.
Al isn't converting any text. 9 is the ascii code for tab. He's just using (chr 9) instead of "\t".

As such, there will be no difference in the outcome since:
Code - Auto/Visual Lisp: [Select]
  1. _$ (= "\t" (chr 9))
  2. T
Title: Re: How save a file as unicode
Post by: 57gmc on November 05, 2019, 04:12:33 PM
Looks like I would have to convert each letter using this method.
Al isn't converting any text. 9 is the ascii code for tab. He's just using (chr 9) instead of "\t".

As such, there will be no difference in the outcome since:
Code - Auto/Visual Lisp: [Select]
  1. _$ (= "\t" (chr 9))
  2. T
Good point. But since Al is using writeline instead of princ, that should produce the proper format for a txt file, correct?
Title: Re: How save a file as unicode
Post by: BIGAL on November 05, 2019, 08:16:03 PM
Thought I better test and worked in excel, recognised the tabs.

A Bit more

Code: [Select]
(setq fo (open (setq fname "d:\\acadtemp\\testab.txt") "w"))
(write-line (strcat "a" (chr 9) "b" (Chr 9) "c") fo)
(write-line (strcat "a" (chr 9) (chr 9) "b" (Chr 9) (chr 9) "c") fo)
(write-line (strcat "f" (chr 9) (chr 9) "g" (Chr 9) (chr 9) "h" (chr 13) "d" (chr 9) (chr 9) "e") fo)
(close fo)
(command "start" "Excel d:\\acadtemp\\testab.txt")