Erlang 从文件中读取前 5 行

发布于 2024-10-15 21:22:32 字数 71 浏览 2 评论 0原文

例如,我有 txt 文件,其中包含 10 串文本。我如何用 erlang 读取此文本的前 5 个字符串?

谢谢。

For example i have txt file with 10 string of text. How can i read first 5 string of this text with erlang?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤云独去闲 2024-10-22 21:22:32

也许您想要 file:open/2file:read_line/1 启用缓冲。

押韵:

$ cat mary_lamb.txt
Mary had a little lamb,
little lamb, little lamb,
Mary had a little lamb,
whose fleece was white as snow.
And everywhere that Mary went,
Mary went, Mary went,
and everywhere that Mary went,
the lamb was sure to go.

源文件:

$ cat ./read_n_lines.erl
-module(read_n_lines).
-export([read_n_lines/2]).

read_n_lines(Filename,NumLines) ->
     {ok, FileDev} = file:open(Filename, 
          [raw, read, read_ahead]),
     Lines = do_read([],FileDev, NumLines),
     file:close(FileDev),
     Lines.

do_read(Lines, _, 0) ->
     lists:reverse(Lines);
do_read(Lines, FileDev, L) ->
     case file:read_line(FileDev) of
          {ok, Line} ->
               do_read([Line|Lines], FileDev, L - 1);
          eof ->
               do_read(Lines, FileDev, 0)
     end.

raw,在Modes中,传递给file:open/2,允许更快地访问文件,因为没有需要 Erlang 进程来处理该文件。

示例运行:

$ erl
1> c(read_n_lines).
{ok,read_n_lines}
2> Lines = read_n_lines:read_n_lines("./mary_lamb.txt", 5).
["Mary had a little lamb,\n","little lamb, little lamb,\n",
 "Mary had a little lamb,\n",
 "whose fleece was white as snow.\n",
 "And everywhere that Mary went,\n"]
3> length(Lines).
5
4> read_n_lines:read_n_lines("./mary_lamb.txt", 666).
["Mary had a little lamb,\n","little lamb, little lamb,\n",
 "Mary had a little lamb,\n",
 "whose fleece was white as snow.\n",
 "And everywhere that Mary went,\n",
 "Mary went, Mary went,\n",
 "and everywhere that Mary went,\n",
 "the lamb was sure to go."]
5> 

要从字符串中删除换行符,您可以使用 string :strip/1,2,3:

5> lists:map(fun(X) -> string:strip(X, right, $\n) end, Lines).
["Mary had a little lamb,","little lamb, little lamb,",
 "Mary had a little lamb,",
 "whose fleece was white as snow.",
 "And everywhere that Mary went,"]
6>

Probably you want a combination of file:open/2 and file:read_line/1 with buffering enabled.

A rhyme:

$ cat mary_lamb.txt
Mary had a little lamb,
little lamb, little lamb,
Mary had a little lamb,
whose fleece was white as snow.
And everywhere that Mary went,
Mary went, Mary went,
and everywhere that Mary went,
the lamb was sure to go.

Source file:

$ cat ./read_n_lines.erl
-module(read_n_lines).
-export([read_n_lines/2]).

read_n_lines(Filename,NumLines) ->
     {ok, FileDev} = file:open(Filename, 
          [raw, read, read_ahead]),
     Lines = do_read([],FileDev, NumLines),
     file:close(FileDev),
     Lines.

do_read(Lines, _, 0) ->
     lists:reverse(Lines);
do_read(Lines, FileDev, L) ->
     case file:read_line(FileDev) of
          {ok, Line} ->
               do_read([Line|Lines], FileDev, L - 1);
          eof ->
               do_read(Lines, FileDev, 0)
     end.

raw, in Modes, passed to the file:open/2, allows faster access to a file, because no Erlang process is needed to handle the file.

Sample run:

$ erl
1> c(read_n_lines).
{ok,read_n_lines}
2> Lines = read_n_lines:read_n_lines("./mary_lamb.txt", 5).
["Mary had a little lamb,\n","little lamb, little lamb,\n",
 "Mary had a little lamb,\n",
 "whose fleece was white as snow.\n",
 "And everywhere that Mary went,\n"]
3> length(Lines).
5
4> read_n_lines:read_n_lines("./mary_lamb.txt", 666).
["Mary had a little lamb,\n","little lamb, little lamb,\n",
 "Mary had a little lamb,\n",
 "whose fleece was white as snow.\n",
 "And everywhere that Mary went,\n",
 "Mary went, Mary went,\n",
 "and everywhere that Mary went,\n",
 "the lamb was sure to go."]
5> 

To remove newline from a string, you can use string:strip/1,2,3:

5> lists:map(fun(X) -> string:strip(X, right, $\n) end, Lines).
["Mary had a little lamb,","little lamb, little lamb,",
 "Mary had a little lamb,",
 "whose fleece was white as snow.",
 "And everywhere that Mary went,"]
6>
遗忘曾经 2024-10-22 21:22:32

另一种解决方案,n_times可以在其他地方使用:

-module(n_times).

-export([test/0]).

test() ->
  io:format("~p~n", [n_lines("n_times.erl", 5)]).

n_lines(FileName, N) ->
  {ok, FileDev} = file:open(FileName, [raw, read, read_ahead]),
  try
    n_times(fun() -> {ok, L} = file:read_line(FileDev), L end, N)
  after
    file:close(FileDev)
  end.

n_times(F, N) ->
  n_times(F, N, []).

n_times(_, 0, A) ->
  lists:reverse(A);
n_times(F, N, A) ->
  n_times(F, N-1, [F()|A]).

Another solution, n_times can be used elsewhere:

-module(n_times).

-export([test/0]).

test() ->
  io:format("~p~n", [n_lines("n_times.erl", 5)]).

n_lines(FileName, N) ->
  {ok, FileDev} = file:open(FileName, [raw, read, read_ahead]),
  try
    n_times(fun() -> {ok, L} = file:read_line(FileDev), L end, N)
  after
    file:close(FileDev)
  end.

n_times(F, N) ->
  n_times(F, N, []).

n_times(_, 0, A) ->
  lists:reverse(A);
n_times(F, N, A) ->
  n_times(F, N-1, [F()|A]).
野味少女 2024-10-22 21:22:32

使用erlang的io模块。

io:读取(FD,'')。

其中 FD 是文件句柄。

另外请查找 erlang 文档以获取正确的语法。

这是一个粗略的代码

func(FD) ->
case io:get_line(FD,'') of
{ok,text}->
 %%do something,
func(FD);
eof ->
%%exit;
error->
%%quit
end

如果您只想处理 10 行,可以使用计数器

use io module of erlang.

io:read(FD,'').

Where FD is the File handle.

Also please do lookup the erlang doc for the correct syntax.

Here is a rough code

func(FD) ->
case io:get_line(FD,'') of
{ok,text}->
 %%do something,
func(FD);
eof ->
%%exit;
error->
%%quit
end

You can use a counter if you want to process just 10 lines

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文