【问题标题】:What is the equivalent of python's with open(file) in julia [duplicate]python 在 julia 中与 open(file) 的等价物是什么 [重复]
【发布时间】:2021-03-05 16:00:21
【问题描述】:

在 Julia 中是否有相当于 python 的 with open() 成语?

>>> with open(path, "r") as file:
...    file.readlines()
['beep\n', 'boop\n']

【问题讨论】:

    标签: julia file-handling


    【解决方案1】:

    我不知道最惯用的方式是什么,但您有几种选择:

    julia> readlines("tmp.log")
    2-element Array{String,1}:
     "beep"
     "boop"
    
    julia> read(file, String)
    "beep\nboop\n"
    
    julia> open("tmp.log", "r") do file
               while !eof(file)
                   @show readline(file)
               end
           end
    readline(file) = "beep"
    readline(file) = "boop"
    
    julia> for line in eachline("tmp.log")
               @show line
           end
    line = "beep"
    line = "boop"
    
    

    请参阅docs 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2017-10-26
      • 2021-02-17
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      • 2016-08-04
      • 2016-07-04
      • 2011-02-13
      • 1970-01-01
      相关资源
      最近更新 更多