Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,9 @@ file = CSV.File(IOBuffer(data); dateformat="yyyy/mm/dd")
```julia
using CSV

# In many places in the world, floating point number decimals are separated with a comma instead of a period (`3,14` vs. `3.14`)
# . We can correctly parse these numbers by passing in the `decimal=','` keyword argument. Note that we probably need to
# explicitly pass `delim=';'` in this case, since the parser will probably think that it detected `','` as the delimiter.
# In many places in the world, floating point number decimals are separated with a comma instead of a period (`3,14` vs. `3.14`).
# We can correctly parse these numbers by passing in the `decimal=','` keyword argument. If the file has no header or
# delimiter detection is otherwise ambiguous, pass `delim=';'` explicitly so commas are treated only as decimal markers.
data = """
col1;col2;col3
1,01;2,02;3,03
Expand Down
25 changes: 19 additions & 6 deletions src/detection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,25 @@ function detectdelimandguessrows(buf, headerpos, datapos, len, oq, eq, cq, @nosp
if delim == UInt8('\n')
if nlines > 0
d = UInt8('\n')
for attempted_delim in (UInt8(','), UInt8('\t'), UInt8(' '), UInt8('|'), UInt8(';'), UInt8(':'))
cnt = bvc.counts[Int(attempted_delim) + 1]
# @show Char(attempted_delim), cnt, nlines
if cnt > 0 && cnt % nlines == 0
d = attempted_delim
break
if headerpos > 0
# Prefer delimiters represented in the header over incidental data characters.
for attempted_delim in (UInt8(','), UInt8('\t'), UInt8(' '), UInt8('|'), UInt8(';'), UInt8(':'))
hcnt = headerbvc.counts[Int(attempted_delim) + 1]
cnt = bvc.counts[Int(attempted_delim) + 1]
if hcnt > 0 && cnt > 0 && cnt % nlines == 0
d = attempted_delim
break
end
end
end
if d == UInt8('\n')
for attempted_delim in (UInt8(','), UInt8('\t'), UInt8(' '), UInt8('|'), UInt8(';'), UInt8(':'))
cnt = bvc.counts[Int(attempted_delim) + 1]
# @show Char(attempted_delim), cnt, nlines
if cnt > 0 && cnt % nlines == 0
d = attempted_delim
break
end
end
end
if d == UInt8('\n')
Expand Down
6 changes: 6 additions & 0 deletions test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ f = CSV.File(IOBuffer("x\ty\n1\t\n2\t\n"))
f = CSV.File(IOBuffer("x|y\n1|\n2|\n"))
@test (length(f), length(f.names)) == (2, 2)

f = CSV.File(IOBuffer("a;b;c\n1,1;2,2;3,3\n4,4;5,5;6,6\n"); decimal=',')
@test (length(f), length(f.names)) == (2, 3)
@test f.a == [1.1, 4.4]
@test f.b == [2.2, 5.5]
@test f.c == [3.3, 6.6]

# type promotion
# int => float
f = CSV.File(IOBuffer("x\n1\n3.14"))
Expand Down
Loading