Reading a txt. file with two different delimiters

Hey guys,

ich want to read in the following text file.
Bildschirmfoto 2022-05-15 um 10.51.40

So the first two rows are irrelevant, but the problem is that the file has space and ; as a delimiter. If I try to read in the file I get the following result:

Now the problem is that the third column is now a string because the integer and the ; gets combined in a string.

Does someone has an idea of how to read in a file like this properly?

Thanks!

If you don’t need to use CSV package in particular, with readdlm from DelimitedFiles you could do
readdlm(filename, Int, skipstart=2, comments=true, comment_char=';')
It will read anything after a ; character (included) as a comment and skip it.

I don’t know much about CSV but it seems it to have optional arguments for comments that you might try to use in the same way: Reading · CSV.jl
Although here it says that the comment character should be at the start of the line…

1 Like

Thank you! That works for me.

Yes I’ve seen the optional arguments for CSV.File but none of these options worked in my case. At least I haven’t found the right one I guess.

Related:

https://github.com/JuliaData/CSV.jl/issues/956

You could try (this is likely only useful to you, faster, if your file is huge):

CSV.File(filname; delim=' ', decimal = ';')

It’s a bit of a hack, with decimal, used as a comments hack, and then make sure your values are actually always integer going in (and what you get). If you tried with the comment keyword arg and it didn’t work then likely because:

Maybe that restriction should be lifted for compatibility with readdlm (which is leaving the stdlib, seems though will be a transparent change to users):

See also this related post and its solution.

Adapted here for reference:

using DelimitedFiles, DataFrames

input2="""
6
0
0 0 1; 4
0 0 2; 5
0 0 3; 6
"""
io = IOBuffer(input2)
data = readdlm(IOBuffer(replace(read(io, String), ";" => " ")), skipstart=2)  # trick by @GunnarFarneback
df = DataFrame(data, :auto)
1 Like