Reputation: 95
I need to read a text file which contains csv
data with headers separating individual blocks of data. The headers always start with the dollar sign $
. So my text file looks like:
$Header1
2
1,2,3,4
2,4,5,8
$Header2
2
1,1,0,19,9,8
2,1,0,18,8,7
What I want to do is if the program reaches to $Header2
, I want to read all the next lines following it till it reaches, say, $Header3
or end of the file. I think I can use `cmp' in Julia for this. I tried with a small file that contains following text:
# file julia.txt
Julia
$Julia
and my code reads:
# test.jl
fname = "julia.txt"
# set some string values
str1 ="Julia";
str2 ="\$Julia";
# print the strings and check the length
println(length(str1),",",str1);
println(length(str2),",",str2);
# now read the text file to check if you are able to find the strings
# str1 and str2 above
println ("Reading file...");
for ln in eachline(fname)
println(length(ln),",",ln);
if (cmp(str1,ln)==0)
println("Julia match")
end
if (cmp(str2,ln)==0)
println("\$Julia match")
end
end
what I get as output from the above code is:
5,Julia
6,$Julia
Reading file...
6,Julia
7,$Julia
I don't understand why I get character length of 6 for string Julia
and 7 for the string $Julia
when they are read from the file. I checked the text file by turning on white spaces and there are none. What am i doing wrong?
Upvotes: 5
Views: 1208
Reputation: 2096
The issue is that the strings returned by eachline
contain a newline character at the end.
You can use chomp
to remove it:
julia> first(eachline("julia.txt"))
"Julia\n"
julia> chomp(first(eachline("julia.txt")))
"Julia"
Also, you can simply use ==
instead of cmp
to test whether two strings are equal. Both use a ccall
to memcmp
but ==
only does that for strings of equal length and is thus probably faster.
Upvotes: 6