【问题标题】:erlang how to print unicode strings?erlang如何打印unicode字符串?
【发布时间】:2011-04-30 18:06:40
【问题描述】:

我使用 io:format 将消息记录到磁盘。每条消息看起来像 [{field1, Content1}, {field2, Content2}, ...]。

当我使用 io:format("~p", [Msg]) 打印它时,文件看起来像 [{field1, >}, ...]。

但我想以原始形式打印 unicode 字符串,而不是整数数组。我该怎么办?

【问题讨论】:

    标签: unicode erlang


    【解决方案1】:

    butter71 是正确的,您不能只打印出该术语并将二进制文件解释为 Unicode。您必须首先隔离二进制文件。打印二进制文件时,您还需要使用“t”,这将允许您打印 latin1 范围之外的字符。见:http://www.erlang.org/doc/man/io_lib.html#format-2

    这是一个打印出你所拥有的东西的例子。我使用 unicode:characters_to_binary 将我输入的内容转换为 UTF8。只是做 > 会导致异常。

    Msg = [{field1, unicode:characters_to_binary("¿,©,ō")}, {field2, ...}, ...]
    [{field1, Field1}|_] = Msg.
    io:format("~ts~n", [Field1]).
    ¿,©,ō
    ok
    io:format("~s~n", [Field1]). 
    ¿,©,Å
    ok
    

    如您所见,没有 't' 的示例会产生乱码。

    如果您要尝试遍历结构以将其转换为字符串并打印出来,请查看 iolists。

    【讨论】:

      【解决方案2】:

      只使用“~s”而不是“~p”可能会成功。

      还可以查看 unicode 模块进行转换—— http://erldocs.com/R14B/stdlib/unicode.html

      编辑:我再次阅读了您的问题并意识到您想要打印整个结构。你可能必须先把它拆开,我不认为 ~p 会做你想做的事。

      【讨论】:

        猜你喜欢
        • 2014-08-23
        • 2015-11-16
        • 2012-06-11
        • 2013-11-23
        • 1970-01-01
        • 1970-01-01
        • 2018-10-08
        • 2018-06-22
        相关资源
        最近更新 更多