如何在 Julia 中逐行读取文件? [英] How to read a file line by line in Julia?

查看:34
本文介绍了如何在 Julia 中逐行读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打开文本文件并逐行读取?我对两种不同的情况感兴趣:

How do I open a text file and read it line by line? There are two different cases I'm interested in answers for:

  1. 一次获取数组中的所有行.
  2. 一次处理每一行.

对于第二种情况,我不想一次将所有行都保存在内存中.

For the second case I don't want to have to keep all the lines in memory at one time.

推荐答案

将文件作为行数组一次全部读入内存只是对 readlines 函数的调用:

Reading a file into memory all at once as an array of lines is just a call to the readlines function:

julia> words = readlines("/usr/share/dict/words")
235886-element Array{String,1}:
 "A"
 "a"
 "aa"
 ⋮
 "zythum"
 "Zyzomys"
 "Zyzzogeton"

默认情况下,这会丢弃换行符,但如果您想保留它们,可以传递关键字参数 keep=true:

By default this discards the newlines but if you want to keep them, you can pass the keyword argument keep=true:

julia> words = readlines("/usr/share/dict/words", keep=true)
235886-element Array{String,1}:
 "A
"
 "a
"
 "aa
"
 ⋮
 "zythum
"
 "Zyzomys
"
 "Zyzzogeton
"

如果你有一个已经打开的文件对象,你也可以将它传递给 readlines 函数:

If you have an already opened file object you can also pass that to the readlines function:

julia> open("/usr/share/dict/words") do io
           readline(io) # throw out the first line
           readlines(io)
       end
235885-element Array{String,1}:
 "a"
 "aa"
 "aal"
 ⋮
 "zythum"
 "Zyzomys"
 "Zyzzogeton"

这演示了 readline 函数,该函数从打开的 I/O 对象中读取一行,或者在给定文件名时打开文件并从中读取第一行:

This demonstrates the readline function, which reads a single line from an open I/O object, or when given a file name, opens the file and reads the first line from it:

julia> readline("/usr/share/dict/words")
"A"

如果您不想一次加载所有文件内容(或者如果您正在处理来自网络套接字的流数据),那么您可以使用 eachline 函数来获取一次生成一行的迭代器:

If you don't want to load the file contents all at once (or if you're processing streaming data like from a network socket), then you can use the eachline function to get an iterator that produces lines one at a time:

julia> for word in eachline("/usr/share/dict/words")
           if length(word) >= 24
               println(word)
           end
       end
formaldehydesulphoxylate
pathologicopsychological
scientificophilosophical
tetraiodophenolphthalein
thyroparathyroidectomize

eachline 函数也可以像 readlines 一样被赋予一个打开的文件句柄来读取行.您还可以通过打开文件并重复调用 readline 来滚动自己的"迭代器:

The eachline function can, like readlines, also be given an opened file handle to read lines from. You can also "roll your own" iterator by opening the file and calling readline repeatedly:

julia> open("/usr/share/dict/words") do io
           while !eof(io)
               word = readline(io)
               if length(word) >= 24
                   println(word)
               end
           end
       end
formaldehydesulphoxylate
pathologicopsychological
scientificophilosophical
tetraiodophenolphthalein
thyroparathyroidectomize

这相当于 eachline 为您做的事情,很少需要自己做,但如果您需要,能力就在那里.有关逐个字符读取文件的更多信息,请参阅此问答:我们如何使用 julia 一次读取一个 .txt 文件的每个字符?

This is equivalent to what eachline does for you and it's rare to need to do this yourself but if you need to, the ability is there. For more information about reading a file character by character, see this question and answer: How do we use julia to read through each character of a .txt file, one at a time?

这篇关于如何在 Julia 中逐行读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆