Noa*_*nck 7 io file character julia
我试图通过Julia的.txt文件,我需要能够在程序读取文件时查看每个字符.我在Julia Docs页面上发现的一点是如何逐行阅读.我知道基本设置应该是这样的
file = open("testfile.txt","r");
while !eof(file)
//look at each character and store it to a variable
Run Code Online (Sandbox Code Playgroud)
一旦将它存储到变量中,我知道如何操作它,但我无法弄清楚如何将它放入变量存储中.
Bog*_*ski 10
使用这样的read功能:
file = open("testfile.txt","r")
while !eof(file)
c = read(file, Char)
# your stuff
end
close(file)
Run Code Online (Sandbox Code Playgroud)
这将使用UTF-8逐个字符地读取它.
如果要逐字节读取它,请使用:
file = open("testfile.txt","r")
while !eof(file)
i = read(file, UInt8)
# your stuff
end
close(file)
Run Code Online (Sandbox Code Playgroud)
请注意,您可以使用do块在离开时自动关闭文件:
open("testfile.txt","r") do file
while !eof(file)
i = read(file, UInt8)
# your stuff
end
end
Run Code Online (Sandbox Code Playgroud)
有关更完整的示例,您可能希望查看此函数https://github.com/bkamins/Nanocsv.jl/blob/master/src/csvreader.jl#L1,它使用模式read(io, Char)来解析CSV文件.