Author Topic: How save a file as unicode  (Read 218 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1062
How save a file as unicode
« 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?

Vaidas

  • Newt
  • Posts: 56
Re: How save a file as unicode
« Reply #1 on: November 04, 2019, 01:30:19 PM »
Try to convert using iconv https://en.wikipedia.org/wiki/Iconv
(mapcar 'chr '(107 105 116 111 120 46 99 111 109))

Lee Mac

  • Seagull
  • Posts: 12282
  • London, England
Re: How save a file as unicode
« Reply #2 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 demonstrates how to read/write Unicode files.

dubb

  • Swamp Rat
  • Posts: 1062
Re: How save a file as unicode
« Reply #3 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 demonstrates how to read/write Unicode files.

BIGAL

  • Bull Frog
  • Posts: 345
  • 30 + years of using Autocad
Re: How save a file as unicode
« Reply #4 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

« Last Edit: November 04, 2019, 07:40:57 PM by BIGAL »
A man who never made a mistake never made anything

dubb

  • Swamp Rat
  • Posts: 1062
Re: How save a file as unicode
« Reply #5 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

57gmc

  • Newt
  • Posts: 93
Re: How save a file as unicode
« Reply #6 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".

Lee Mac

  • Seagull
  • Posts: 12282
  • London, England
Re: How save a file as unicode
« Reply #7 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

57gmc

  • Newt
  • Posts: 93
Re: How save a file as unicode
« Reply #8 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?

BIGAL

  • Bull Frog
  • Posts: 345
  • 30 + years of using Autocad
Re: How save a file as unicode
« Reply #9 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")
A man who never made a mistake never made anything