[英]What is the best way to read TXT file content and parse it into Erlang tuples?
I've got a TXT file in the form of tuples:我有一个元组形式的 TXT 文件:
{187,386,858}.
{46,347,621}.
{28,856,354}.
{246,298,574}.
{979,964,424}.
{664,722,318}.
{82,69,64}.
I need to read the file, so i get a list of usable tuples to pass {Key, Num1, Num2}
to a function.我需要读取文件,所以我得到了一个可用元组列表来将{Key, Num1, Num2}
传递给一个函数。
I have tried using binary:split
, but that doesn't get me quite there:我试过使用binary:split
,但这并没有让我完全在那里:
{ok, Device} = file:read_file(Filename),
Tuples = [binary_to_list(Bin) || Bin <- binary:split(Device,<<".\n">>,[global])].
I will get a result like this:我会得到这样的结果:
["{187,386,858}","{46,347,621}","{28,856,354}",
"{246,298,574}","{979,964,424}","{664,722,318}",
"{82,69,64}","{654,351,856}","{185,101,57}","{93,747,166}",
"{41,442,946}","{444,336,300}","{951,589,376}",
"{193,987,300}",[]]
but looking to get something more in this format:但希望以这种格式获得更多东西:
[{979, 193, 224}, {365, 339, 950}, {197, 586, 308},
{243, 563, 245}, {795, 534, 331}, {227, 736, 701},
{111, 901, 185}, {303, 178, 461}, {361, 212, 985},
{149, 659, 496}, {612, 375, 311}, {896, 402, 10}]
你有file:consult/1直接从文件解析 Erlang 术语。
You can try:你可以试试:
1> Device = <<"{187,386,858}.\n{46,347,621}.\n{28,856,354}.\n{246,298,574}">>.
2> Lists = [binary:split(binary:part(Bin, 1, byte_size(Bin) - 2), <<",">>, [global]) || Bin <- binary:split(Device,<<".\n">>,[global])].
3> Res = [list_to_tuple([binary_to_integer(B) || B <- L]) || L <- Lists].
4> Res.
[{187,386,858},{46,347,621},{28,856,354},{246,298,574}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.