Julia - 文件 I/O

从文件中读取

open()、read() 和 close() 函数是从文本文件中提取信息的标准方法。

打开文本文件

如果要从文本文件中读取文本,需要首先获取文件句柄。 可以借助 open() 函数来完成,如下所示 −

foo = open("C://Users//Leekha//Desktop//NLP.txt")

它表明现在 foo 是 Julia 与磁盘上的文本文件(即 NLP.txt)的连接。

关闭文件

完成文件后,我们应该关闭连接,如下所示 −

Close(foo)

在 Julia 中,建议将所有文件处理函数包装在 do 块中,如下所示 −

open("NLP.txt") do file
   # here you can work with the open file
end

将文件处理函数包装在 do 块内的优点是,当该块完成时,打开的文件将自动关闭。

保留一些信息的示例,例如读取文件的totaltime(总时间)和文件中的totallines(总行数)

julia> totaltime, totallines = open("C://Users//Leekha//Desktop//NLP.txt") do foo

            linecounter = 0
            timetaken = @elapsed for l in eachline(foo)
               linecounter += 1
            end
            (timetaken, linecounter)
         end
(0.0001184, 87)

一次读取一个文件

使用read()函数,我们可以一次读取一个打开文件的全部内容,例如 −

ABC = read(foo, String)

同样,下面将文件的内容存储在ABC中 −

julia> ABC = open("C://Users//Leekha//Desktop//NLP.txt") do file
            read(file, String)
         end

我们还可以将整个文件作为数组读取。 使用 readlines() 如下 −

julia> foo = open("C://Users//Leekha//Desktop//NLP.txt")
IOStream(<file C://Users//Leekha//Desktop//NLP.txt>)


julia> lines = readlines(foo)
87-element Array{String,1}:
 "Natural Language Processing: Semantic Analysis "
 ""
 "Introduction to semantic analysis:"
"The purpose of semantic analysis is to draw exact meaning, or you can say dictionary meaning from the text. Semantic analyzer checks the text for meaningfulness. "………………………………

逐行阅读

我们还可以逐行处理文件。 对于此任务,Julia 提供了一个名为 eachline() 的函数,它基本上将源转换为迭代器。

julia> open("C://USers/Leekha//Desktop//NLP.txt") do file
         for ln in eachline(file)
            println("$(length(ln)), $(ln)")
         end
      end
47, Natural Language Processing: Semantic Analysis
0,
34, Introduction to semantic analysis:
…………………………………

如果您想在读取文件时跟踪您所在的行,请使用下面给出的方法 −

julia> open("C://Users//Leekha//Desktop//NLP.txt") do f
         line = 1
         while !eof(f)
            x = readline(f)
            println("$line $x")
            line += 1
         end
      end
1 Natural Language Processing: Semantic Analysis
2
3 Introduction to semantic analysis:
4 The purpose of semantic analysis is to draw exact meaning, or you can say dictionary meaning from the text. Semantic analyzer checks the text for meaningfulness.
5 We know that lexical analysis also deals with the meaning of the words then how semantic analysis is different from lexical analysis? The answer is that Lexical analysis is based on smaller token but on the other side semantic analysis focuses on larger chunks. That is why semantic analysis can be divided into the following two parts:
6 Studying the meaning of individual word: It is the first part of the semantic analysis in which the study of the meaning of individual words is performed. This part is called lexical semantics.
7 Studying the combination of individual words: In this second part, the individual words will be combined to provide meaning in sentences.
8 The most important task of semantic analysis is to get the proper meaning of the sentence. For example, analyze the sentence “Ram is great.” In this sentence, the speaker is talking either about Lord Ram or about a person whose name is Ram. That is why the job, to get the proper meaning of the sentence, of semantic analyzer is important.
9 Elements of semantic analysis:
10 Following are the elements of semantic analysis:……………………..

路径和文件名

下表显示了对处理文件名有用的函数 −

Sl.No 函数和描述
1

cd(path)

此函数更改当前目录。

2

pwd()

该函数获取当前工作目录。

3

readdir(path)

此函数返回指定目录或当前目录的内容列表。

4

abspath(path)

此函数将当前目录的路径添加到文件名中以形成绝对路径名。

5

joinpath(str, str, ...)

该函数将路径名片段组合起来。

6

isdir(path)

这个函数告诉你路径是否是一个目录。

7

splitdir(path)

此函数将路径拆分为目录名和文件名的元组。

8

splitdrive(path)

此函数在 Windows 上将路径拆分为驱动器号部分和路径部分。 并且,在 Unix 系统上,第一个组件始终是空字符串。

9

splitext(path)

此函数,如果路径的最后一个组成部分包含点,则将路径拆分为点之前的所有内容以及包含点和点之后的所有内容。 否则,返回未修改的参数和空字符串的元组。

10

expanduser(path)

此函数将路径开头的波形符替换为当前用户的主目录。

11

normpath(path)

此函数标准化路径,删除"." 和".."条目。

12

realpath(path)

此函数通过扩展符号链接并删除 "." 和 ".." 条目来规范化路径。

13

homedir()

此函数给出当前用户的主目录。

14

dirname(path)

此函数获取路径的目录部分。

15

basename(path)

此函数获取路径的文件名部分。

有关文件的信息

我们可以使用stat("pathname")来获取特定文件的信息。

示例

julia> for n in fieldnames(typeof(stat("C://Users//Leekha//Desktop//NLP.txt")))
            println(n, ": ", getfield(stat("C://Users//Leekha//Desktop//NLP.txt"),n))
         end
device: 3262175189
inode: 17276
mode: 33206
nlink: 1
uid: 0
gid: 0
rdev: 0
size: 6293
blksize: 4096
blocks: 16
mtime: 1.6017034024103658e9
ctime: 1.6017034024103658e9

与文件系统交互

如果要将文件名转换为路径名,可以使用abspath()函数。 我们可以将其映射到目录中的文件列表,如下所示 −

julia> map(abspath, readdir())
204-element Array{String,1}:
 "C:\\Users\\Leekha\\.anaconda"
 "C:\\Users\\Leekha\\.conda"
 "C:\\Users\\Leekha\\.condarc"
 "C:\\Users\\Leekha\\.config"
 "C:\\Users\\Leekha\\.idlerc"
 "C:\\Users\\Leekha\\.ipynb_checkpoints"
 "C:\\Users\\Leekha\\.ipython"
 "C:\\Users\\Leekha\\.julia"
 "C:\\Users\\Leekha\\.jupyter"
 "C:\\Users\\Leekha\\.keras"
 "C:\\Users\\Leekha\\.kindle"…………………………

写入文件

函数writedlm()DelimitedFiles包中的函数可用于将对象的内容写入文本文件。

示例

julia> test_numbers = rand(10,10)
10×10 Array{Float64,2}:
 0.457071 0.41895  0.63602  0.812757 0.727214 0.156181 0.023817 0.286904 0.488069 0.232787
 0.623791 0.946815 0.757186 0.822932 0.791591 0.67814 0.903542 0.664997 0.702893 0.924639
 0.334988 0.511964 0.738595 0.631272 0.33401 0.634704 0.175641 0.0679822 0.350901 0.0773231
 0.838656 0.140257 0.404624 0.346231 0.642377 0.404291 0.888538 0.356232 0.924593 0.791257
 0.438514 0.70627 0.642209 0.196252 0.689652 0.929208 0.19364 0.19769 0.868283 0.258201
 0.599995 0.349388 0.22805 0.0180824 0.0226505 0.0838017 0.363375 0.725694 0.224026 0.440138
 0.526417 0.788251 0.866562 0.946811 0.834365 0.173869 0.279936 0.80839 0.325284 0.0737317
 0.0805326 0.507168 0.388336 0.186871 0.612322 0.662037 0.331884 0.329227 0.355914 0.113426
 0.527173 0.0799835 0.543556 0.332768 0.105341 0.409124 0.61811 0.623762 0.944456 0.0490737
 0.281633 0.934487 0.257375 0.409263 0.206078 0.720507 0.867653 0.571467 0.705971 0.11014
 
julia> writedlm("C://Users//Leekha//Desktop//testfile.txt", test_numbers)