file - 我们如何使用julia一次读取一个.txt文件的每个字符?

标签 file io character julia

我试图与Julia一起浏览.txt文件,并且程序读取文件时,我需要能够查看每个字符。我在Julia Docs页面上发现的很少是如何逐行阅读的。我知道基本设置应该是这样的

file = open("testfile.txt","r");
while !eof(file)
    //look at each character and store it to a variable 

一旦将其存储到变量中,我就知道如何操作它,但是我无法弄清楚如何将其存储到变量存储中。

最佳答案

使用read函数,如下所示:

file = open("testfile.txt","r")
while !eof(file)
    c = read(file, Char)
    # your stuff
end
close(file)

这将使用UTF-8逐个字符地读取它。

如果要逐字节读取,请使用:
file = open("testfile.txt","r")
while !eof(file)
    i = read(file, UInt8)
    # your stuff
end
close(file)

请注意,您可以使用do块在离开文件时自动关闭文件:
open("testfile.txt","r") do file
    while !eof(file)
        i = read(file, UInt8)
        # your stuff
    end
end

对于更完整的示例,您可能想看看例如在此功能https://github.com/bkamins/Nanocsv.jl/blob/master/src/csvreader.jl#L1上,它使用模式read(io, Char)解析CSV文件。

关于file - 我们如何使用julia一次读取一个.txt文件的每个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52281241/

相关文章:

c - 当我使用循环时,C如何读取字符和换行符?

C 文件检查文件是否为空或包含 ASCII 文本

java - 文件权限问题

file - 如何获取文件路径以显示在 Netbeans 选项卡上

python - "Upload"来自 django shell 的文件

c++ - 重载 >> 运算符以读取文本文件

c - scanf() 调用后的指令被调用两次

C 编程 : Scanf in while loop only reads input once and then terminates

c - 只读模式下的 fopen() 及其缓冲区

c - C : convert escape sequences into visible ones 的 K&R 中的练习 3-2