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

查看:726
本文介绍了如何在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\n"
 "a\n"
 "aa\n"
 ⋮
 "zythum\n"
 "Zyzomys\n"
 "Zyzzogeton\n"

如果您已经打开了文件对象,也可以将其传递给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为您执行的操作,很少需要自己执行此操作,但是如果需要,此功能就存在.有关逐字符读取文件的更多信息,请参见以下问题和答案:

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天全站免登陆