欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Julia:1.0更新后有关文件的函数

程序员文章站 2022-04-28 09:22:41
...
本文将简要介绍julia1.0发布以后,有关文件的函数介绍
下面的代码为批量产生10个文本文件,并在每个文本文件中写入其文件名的示例代码

using DelimitedFiles

n = 10
filelist = Vector{String}(undef,n)  # 用来存储文件名的字符串数组
for i in 1:n
    filelist[i] = string(i, ".txt")  # 存储文件名
    println( filelist[i] )
end

fileio = [open(filename,"w") for filename in filelist]  # 打开每个文件

for i in 1:length(fileio)
    writedlm( fileio[i], [filelist[i]] )  # 对每个文件写入其文件名
end

map(close, fileio)  # 逐一关掉文件

在julia1.0的REPL中,输入using DelimitedFiles后,再输入?,接着输入writecsv,结果如下:
help?> writecsv
search:

Couldn't find writecsv
Perhaps you meant writedlm, write or withenv
  No documentation found.

  Binding writecsv does not exist.
看起来julia1.0已将writecsv函数废弃了。

输入writedlm,结果如下:
help?> writedlm
search: writedlm

  writedlm(f, A, delim='\t'; opts)

  Write A (a vector, matrix, or an iterable collection of iterable rows) as
  text to f (either a filename string or an IO stream) using the given
  delimiter delim (which defaults to tab, but can be any printable Julia
  object, typically a Char or AbstractString).

  For example, two vectors x and y of the same length can be written as two
  columns of tab-delimited text to f by either writedlm(f, [x y]) or by
  writedlm(f, zip(x, y)).

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> using DelimitedFiles

  julia> x = [1; 2; 3; 4];

  julia> y = [5; 6; 7; 8];

  julia> open("delim_file.txt", "w") do io
             writedlm(io, [x y])
         end

  julia> readdlm("delim_file.txt", '\t', Int, '\n')
  4×2 Array{Int64,2}:
   1  5
   2  6
   3  7
   4  8

  julia> rm("delim_file.txt")

综上就是julia1.0关于文件处理这一块的变化。当然上述只是一小部分,后面有机会会持续更新这一博文!
相关标签: julia