erlang 如何打印 unicode 字符串?
我使用 io:format 将消息记录到磁盘。每条消息看起来都像 [{field1, Content1}, {field2, Content2}, ...]。
当我使用 io:format("~p", [Msg]) 打印它时,文件将看起来像 [{field1, <<123,456,789,...>>}, ...]。
但我想以原始形式打印 unicode 字符串,而不是像整数数组那样。我应该怎么办?
I use io:format to log messages to disk. Each message looks like [{field1, Content1}, {field2, Content2}, ...].
When I use io:format("~p", [Msg]) to print it, the file will look like [{field1, <<123,456,789,...>>}, ...].
But I want to print the unicode strings in their original form, not like integer arrays. What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Butter71 是对的,你不能只打印出该术语并将二进制文件解释为 Unicode。您必须首先隔离二进制文件。打印二进制文件时,您还需要使用“t”,它允许您打印 latin1 范围之外的字符。请参阅:http://www.erlang.org/doc/man/io_lib .html#format-2
这是打印出类似内容的示例。我使用 unicode:characters_to_binary 将我输入的内容转换为 UTF8。只是做<<“¿,©,ō”>>会引发异常。
正如您所看到的,没有“t”的示例会产生乱码文本。
如果您打算尝试遍历您的结构以将其转换为字符串并将其打印出来,请查看 iolists。
butter71 is right that you won't be able to just print out the term and have the binaries interpreted as Unicode. You will have to isolate the binaries first. When printing the binaries, you'll also need to use 't' which will allow you to print characters outside the latin1 range. See: http://www.erlang.org/doc/man/io_lib.html#format-2
Here is an example of printing out something like you have. I use unicode:characters_to_binary to convert what I input to UTF8. Just doing <<"¿,©,ō">> will cause an exception.
As you can see the example without 't' produces garbled text.
If you're going to try and walk through your structure to convert it to a string and print it out, look at iolists.
仅使用“~s”而不是“~p”可能会成功。
还要检查 unicode 模块来进行转换——
http://erldocs.com/R14B/stdlib/unicode.html
编辑:我读再次提出你的问题,并意识到你想要打印整个结构。你可能必须先把它拆开,我不认为〜p会做你想做的事。
You might have success with just using "~s" instead of "~p".
Also check out the unicode module to do conversions --
http://erldocs.com/R14B/stdlib/unicode.html
edit: i read your question again and realize that you want to print the entire structure. you will probably have to break it apart first, i don't think ~p will do what you want.