System: Linux
In python2.7 I do this:
and get: 彼は銀行で働いています。
in Nim 0.10.3: echo ?? how do
Thanks
Um, what's wrong with
echo("彼は銀行で働いています。")
?Thank you for looking at it.
There is nothing wrong with what you did.
Try this:
echo ("u'\u5f7c\u306f\u9280\u884c\u3067\u50cd\u3044\u3066\u3044\u307e\u3059\u3002'")
or any variation of it.
I am getting this string as result from a database cursor and try
to actually display the contents rather than codes.
Not sure,I may mistake, but I am afraid that u".." doesn't exist in Nim
you can transform an unicode char with something like
import unicode
Rune(0x5f7c).toUTF8
the Nim unicode module is far to be complete for the moment (if I compare to say: Elixir unicode module). Probably Nim have yet to evolve in this area. Even runeAt(...) work at area index level and that's bad, but Nim is a new language and have still an important need for contributor
DarkBlue, that's not a unicode string, that's an ASCII string containing some form of unicode escaping sequence. The u prefix suggests you have some python code escaping unicode characters writing to the database and later decoding them when reading, quite a performance problem. Still, you can use libraries like iconv to decode the unicode escape sequences. The following example works under unix:
import encodings
proc test() =
let input = r"u'\u5f7c\u306f\u9280\u884c\u3067\u50cd\u3044\u3066\u3044\u307e\u3059\u3002'"
echo input.convert("UTF-8", "C99")
# --> u'彼は銀行で働いています。'
when isMainModule: test()
The example might fail under Windows if it doesn't support the C99 encoding. In any case, the output still contains the u'…' markers. If you can't get rid of this encoding in the database you will have to write your own escaping of python unicode strings. Or maybe use something like nimborg to do the string decoding.import unicode, strutils
proc escStrToUTF8(s: string): string =
result=newStringOfCap(s.len/%6*4)
for i in 0..(s.len/%6-1):
let c=parseHexInt(s[(i*6+2)..(i*6+5)]).Rune.toUTF8
result.add c
echo escStrToUTF8(r"\u5f7c\u306f\u9280\u884c\u3067\u50cd\u3044\u3066\u3044\u307e\u3059\u3002")
Thank you for your replies, which helped me to understand what's needed.
I am already using nimborg to get the data from a Firebird 2.5.3
database, however, the unpacking of these escaped strings in Nim
had me stumped .
Doing everything on the python side obviously works.