Reading text data: `readdlm` is deprecated, so how CSV package is used?

ryofurue
Fri, 12 Jul 2024 09:15:11 +0000
General Usage

In the issue tracker of DelimitedFiles

github.com/JuliaData/DelimitedFiles.jl

readdlm not working with white spaces

opened 06:14PM - 03 Apr 22 UTC

cdelv

readdlm ignores all-white spaces by default when a delimiter is not specified. H…owever, when one wants to specify the data type to be read it is obligatory to specify the delimiter too... `readdlm(source, delim::AbstractChar, T::Type, eol::AbstractChar; header=false, skipstart=0, skipblanks=true, use_mmap, quotes=true, dims, comments=false, comment_char='#')` Then, in the following case, `readdlm(file, ' ', Float64, comments=true)` the function doesn't ignore the initial whitespace because the delimiter is `' '`, only 1 whitespace. Then the program crashes with for example ``` 2 3 1 3 ``` There should be a flag to ignore all chars that match with the delimiter or just be able to specify the type like this `readdlm(file, type=Float64, comments=true)` however this brings the problem that if the delimiter is not a whitespace the problem will persist.

I found a comment

readdlm is also effectively deprecated and the CSV package should be used

about reading text data files.

I’m trying to read a plain text file listing numbers delimited by newlines and spaces.

Does somebody know where to find a simple tutorial to use the CSV package to do this?

I just blindly tried the CSV package. I got an object containing objects like Vector{CSV.Row} and I didn’t know how to examine whether my read was successful or not.

The data file includes comment lines indicated by “#”. The file can be read with

using DelimitedFiles
a = readdlm("my-datafile.txt"; comments=true)
a[2,5] # -> the number at row 2, column 5

This is a very simple interface. You just get a 2D array. How does one use the CSV package to do this?

Aside: The data file I’m trying to read turns out including the unicode “zero width no-break space”, which readdlm() fails to handle. So, I was trying to do something about it, when I found the deprecation comment above.

10 posts - 4 participants