@@ -66,6 +66,7 @@ Read CSV from `file`. Returns a tuple of 2 elements:
6666- `spacedelim`: (Bool) parse space-delimited files. `delim` has no effect if true.
6767- `quotechar`: character used to quote strings, defaults to `"`
6868- `escapechar`: character used to escape quotechar in strings. (could be the same as quotechar)
69+ - `commentchar`: ignore lines that begin with commentchar
6970- `nrows`: number of rows in the file. Defaults to `0` in which case we try to estimate this.
7071- `skiplines_begin`: skips specified number of lines at the beginning of the file
7172- `header_exists`: boolean specifying whether CSV file contains a header
@@ -157,6 +158,7 @@ function _csvread_internal(str::AbstractString, delim=',';
157158 spacedelim= false ,
158159 quotechar= ' "' ,
159160 escapechar= ' "' ,
161+ commentchar= nothing ,
160162 stringtype= String,
161163 stringarraytype= StringArray,
162164 noresize= false ,
@@ -206,6 +208,11 @@ function _csvread_internal(str::AbstractString, delim=',';
206208 pos, lines = eatnewlines (str, pos)
207209 lineno += lines
208210 end
211+
212+ # Ignore commented lines before the header.
213+ pos, lines = eatcommentlines (str, pos, len, commentchar)
214+ lineno += lines
215+
209216 if header_exists
210217 merged_colnames, pos = readcolnames (str, opts, pos, colnames)
211218 lineno += 1
@@ -235,8 +242,8 @@ function _csvread_internal(str::AbstractString, delim=',';
235242
236243 # seed guesses using those from previous file
237244 guess, pos1 = guesscolparsers (str, canonnames, opts,
238- pos, type_detect_rows, colparsers,
239- stringarraytype , nastrings, prev_parsers)
245+ pos, type_detect_rows, colparsers, stringarraytype,
246+ commentchar , nastrings, prev_parsers)
240247 if isempty (canonnames)
241248 canonnames = Any[1 : length (guess);]
242249 end
@@ -319,7 +326,7 @@ function _csvread_internal(str::AbstractString, delim=',';
319326 @label retry
320327 try
321328 finalrows = parsefill! (str, opts, rec, nrows, cols, colspool,
322- pos, lineno, rowno, lastindex (str))
329+ pos, lineno, rowno, lastindex (str), commentchar )
323330 if ! noresize
324331 resizecols (colspool, finalrows)
325332 end
@@ -470,19 +477,19 @@ function readcolnames(str, opts, pos, colnames)
470477 colnames_inferred, lineend+ 1
471478end
472479
473-
474480function guesscolparsers (str:: AbstractString , header, opts:: LocalOpts , pos:: Int ,
475- nrows:: Int , colparsers, stringarraytype, nastrings= NA_STRINGS, prevs= nothing )
481+ nrows:: Int , colparsers, stringarraytype, commentchar = nothing , nastrings= NA_STRINGS, prevs= nothing )
476482 # Field type guesses
477483 guess = []
478484 prevfields = String[]
479485
480486 givenkeys = ! isempty (colparsers) ? first .(collect (optionsiter (colparsers, header))) : []
481487 for i2= 1 : nrows
482488 pos, _ = eatnewlines (str, pos)
483- if pos > lastindex (str)
484- break
485- end
489+
490+ # Move past commented lines before guessing.
491+ pos, _ = eatcommentlines (str, pos, lastindex (str), commentchar)
492+ pos > lastindex (str) && break
486493
487494 lineend = getrowend (str, pos, lastindex (str), opts, opts. endchar)
488495
@@ -532,12 +539,19 @@ function guesscolparsers(str::AbstractString, header, opts::LocalOpts, pos::Int,
532539end
533540
534541function parsefill! (str:: AbstractString , opts, rec:: RecN{N} , nrecs, cols, colspool,
535- pos, lineno, rowno, l= lastindex (str)) where {N}
542+ pos, lineno, rowno, l= lastindex (str), commentchar = nothing ) where {N}
536543 pos, lines = eatnewlines (str, pos, l)
537544 lineno += lines
545+
538546 pos <= l && while true
539547 prev_j = pos
540548 lineno += lines
549+
550+ # Do not try to parse commented lines.
551+ pos, lines = eatcommentlines (str, pos, l, commentchar)
552+ lineno += lines
553+ pos > l && return rowno- 1
554+
541555 res = tryparsesetindex (rec, str, pos, l, cols, rowno, opts)
542556 if ! issuccess (res)
543557 pos, fieldpos, colno, err_code = geterror (res)
@@ -553,6 +567,7 @@ function parsefill!(str::AbstractString, opts, rec::RecN{N}, nrecs, cols, colspo
553567 if pos > l
554568 return rowno
555569 end
570+
556571 rowno += 1
557572 lineno += 1
558573 if rowno > nrecs
0 commit comments