Erlang:奇怪的输出格式

时间:2015-05-05 12:15:14

标签: xml file formatting erlang output

Windows 7 x64,Erlang-OTP 17。 我写了这样简单的模块:

-module (somequery).
-export ([fbquery/2]).

fbquery(P1,P2) ->  
    inets:start(),
    ssl:start(),
    token = "78a8shd67tyajsndweiu03hr83h19j",
    Encoded = {"Authorization","Basic " ++ base64:encode_to_string(lists:append([token,":",""]))},
    ContentType = "application/xml",
    Headers = [Encoded, {"Content-Type",ContentType}],
    Options = [{body_format,binary}],
    {ok, File}=file:read_file(P1),
    Res = httpc:request(post, {"https://datapi.com/api/xml4-8", Headers, ContentType, File}, [], Options),
    file:write_file(P2, io_lib:fwrite("~p.\n", [Res])).

它既可用于交互式,也可用于编译,而Res术语则用于显示数据

59> Res.                                                                                                     
{ok,{{"HTTP/1.1",200,"OK"},
     [{"connection","keep-alive"},
      {"date","Tue, 05 May 2015 10:58:53 GMT"},
      {"server","nginx"},
      {"vary","Accept-Encoding"},
      {"content-length","5508"},
      {"content-type","application/xml; charset=utf-8"},
      {"x-frame-options","SAMEORIGIN"},
      {"p3p",
       "CP=\"To see our privacy policy, go here: http://www.datapi.com/policies/privacy\""},
      {"strict-transport-security","max-age=31536000"}],
     <<"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<response xmlns=\"http://www.datapi.com/api/\" status=\""...>>}}

但是在输出文件(P2 param)中我看到了这个

{ok,{{"HTTP/1.1",200,"OK"},
     [{"connection","keep-alive"},
      {"date","Tue, 05 May 2015 10:58:53 GMT"},
      {"server","nginx"},
      {"vary","Accept-Encoding"},
      {"content-length","5508"},
      {"content-type","application/xml; charset=utf-8"},
      {"x-frame-options","SAMEORIGIN"},
      {"p3p",
       "CP=\"To see our privacy policy, go here: http://www.datapi.com/policies/privacy\""},
      {"strict-transport-security","max-age=31536000"}],
     <<60,63,120,109,108,32,118,101,114,115,105,111,110,61,34,49,46,48,34,32,
       101,110,99,111,100,105,110,103,61,34,117,116,102,45,56,34,63,62,10,60,
       114,101,115,112,111,110,115,101,32,120,109,108,110,115,61,34,104,116,
       116,112,58,47 ... MORE NUMBERS HERE ....101,62,10>>}}.

那很奇怪,我记得在完全互动模式下我没有这样的问题。有什么建议吗?

UPD:这很有意思,但只有在收到的XML包含非ASCII(非拉丁文?)字符时才会出现问题。在其他情况下,文件中的所有XML都是正确的。

1 个答案:

答案 0 :(得分:1)

那是因为运行时不确定您的终端是否可以显示非ASCII unicode。所有字符串都只是整数列表,所有二进制文件都只是分成8位字节的长字符串。所以你看到的数字你想看到的数据,只是它的原始形式。

要以您希望的方式显示,请尝试使用io:format/2~tp替换而不是~p替换的显示功能。在您的情况下,写入文件,您可能需要这样做:

write(Filename, UTF8_data) ->
    file:write_file(Filename, unicode:characters_to_binary(UTF8_data, utf8)).

read(Filename) ->
    case file:read_file(Filename) of
        {ok, Data} -> {ok, unicode:characters_to_list(Data, utf8)};
        Other      -> Other
    end.

请参阅文档中的Using Unicode in Erlang。它比以前更好,但仍然有点烦人(所以将它包装在一些函数中,如上面的read / 1和write / 2)。

那就是说... Windows在unicode(以及一般的编码)方面有其独特的愚蠢形式,我的大部分Erlang经验都是在Linux和BSD上 - 我在Windows上做的唯一事情就是客户端GUI Wx中的事情。