user3086855
user3086855

Reputation: 21

How can I read text file with converting in Julia?

I try to read a text file in julia but I cannot give any type while reading it gives an error;

data = readdlm("data.txt",'\t', Float64)

at row 1, column 1 : ErrorException("file entry \" 0.00632 18.00 2.310 0 0.5380 6.5750 65.20 4.0900 1 296.0 15.30 396.90 4.98 24.00\" cannot be converted to Float64")

If I dont use Float64, the data type is Array{Any,2}. this result returns but I have 14 different columns in the data.

 " 0.27957   0.00   9.690  0  0.5850  5.9260  42.60  2.3817   6  391.0  19.20 396.90  13.59  24.50"
 " 0.17899   0.00   9.690  0  0.5850  5.6700  28.80  2.7986   6  391.0  19.20 393.29  17.60  23.10"
 " 0.28960   0.00   9.690  0  0.5850  5.3900  72.90  2.7986   6  391.0  19.20 396.90  21.14  19.70"
 " 0.26838   0.00   9.690  0  0.5850  5.7940  70.60  2.8927   6  391.0  19.20 396.90  14.10  18.30"
 " 0.23912   0.00   9.690  0  0.5850  6.0190  65.30  2.4091   6  391.0  19.20 396.90  12.92  21.20"

Upvotes: 1

Views: 1103

Answers (1)

David Sainez
David Sainez

Reputation: 6956

I recommend using the CSV library to parse delimited files. It has features, such as handling repeated delimiters, which will probably deal with your input file.

julia> using Pkg

julia> Pkg.add("CSV")

julia> import CSV

julia> Array(CSV.read("data.txt"; delim=' ', ignorerepeated=true, type=Float64))
4×14 Array{Float64,2}:
 0.17899  0.0  9.69  0.0  0.585  5.67   28.8  2.7986  6.0  391.0  19.2  393.29  17.6   23.1
 0.2896   0.0  9.69  0.0  0.585  5.39   72.9  2.7986  6.0  391.0  19.2  396.9   21.14  19.7
 0.26838  0.0  9.69  0.0  0.585  5.794  70.6  2.8927  6.0  391.0  19.2  396.9   14.1   18.3
 0.23912  0.0  9.69  0.0  0.585  6.019  65.3  2.4091  6.0  391.0  19.2  396.9   12.92  21.2


Upvotes: 2

Related Questions