# CSV.jl number of lines

**URL:** <https://discourse.julialang.org/t/csv-jl-number-of-lines/70785>\
**Category:** General Usage\
**Tags:** csv, io\
**Created:** [November 2, 2021, 7:55am UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785 "2021-11-02T07:55:55Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)\
**Post date:** [November 2, 2021, 7:55am UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/1 "2021-11-02T07:55:55Z")

</div>

I cannot use attribute “resusebuffer” in latest version of CSV.jl package. How can I calculate exact number of lines in CSV?

```julia
function countcsvlines(file)
    n = 0
    for row in CSV.Rows(file; resusebuffer=true)
        n += 1
    end
    return n
end

```

I tried to use:

```julia
CSV.Rows(fl.buf).ctx.rowsguess

```

but it gives me higher numbers. File with 5 lines has result 7 and file with 52 lines has result 57.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 2, 2021, 8:34am UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/2 "2021-11-02T08:34:59Z")

</div>

> [@Michal](#):
>
> `CSV.Rows(file; resusebuffer=true)`

Did you mean to write `reusebuffer` instead of `resusebuffer` (note the extra `s` in “re **s** use”)?

Other than that, you may need to set the `header` keyword, depending on whether or not your dataset has a header with column names or not (there may also be other metadata in front of the header, depending on the CSV). Other than that, it works for me:

```julia
julia> function countcsvlines(file)
           n = 0
           for row in CSV.Rows(file; header=false, reusebuffer=true)
               n += 1
           end
           return n
       end
countcsvlines (generic function with 1 method)

julia> countcsvlines("example.csv")
8

julia> readlines("example.csv")
8-element Vector{String}:
 "1,2,3,4"
 "4,5,6,7"
 "1,2,3,4"
 "4,5,6,7"
 "1,2,3,4"
 "4,5,6,7"
 "1,2,3,4"
 "4,5,6,7"

```

---

<div class="post-metadata">

**Author:** ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)\
**Post date:** [November 2, 2021, 8:42am UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/3 "2021-11-02T08:42:15Z")

</div>

Thank you, I meant `reusebuffer`. My fault.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 2, 2021, 8:51am UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/4 "2021-11-02T08:51:31Z")

</div>

> [@Michal](#):
>
> How can I calculate exact number of lines in CSV?

Just in case, there is a built-in function:

```julia
file = raw"C:\...\input.csv"
countlines(file)

```

---

<div class="post-metadata">

**Author:** ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)\
**Post date:** [November 2, 2021, 8:53am UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/5 "2021-11-02T08:53:43Z")

</div>

Thank you @rafael.guerra . Is it possible to do something like following code?

```julia
usng CSV
csvRows = CSV.Rows("example.csv")
countlines(csvRows.buf)

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 2, 2021, 9:12am UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/6 "2021-11-02T09:12:20Z")

</div>

> [@rafael.guerra](#):
>
> ```julia
> file = raw"C:\...\input.csv"
> countlines(file)
> 
> ```

That won’t work if the CSV contains a quoted field with a newline character - CSV.jl accounts for that.

> [@Michal](#):
>
> Is it possible to do something like following code?

CSV.Rows is a lazy iterator

> CSV.Rows: an alternative approach for consuming delimited data, where the input is only consumed one row at a time

As such, you either have to iterate manually or `collect` it to figure out how many rows there are.

You can’t get around that limitation simply because CSV is much more complicated than you may initially assume - it’s not standardized, different escaping mechanisms exist and a newline character may not always indicate a new row.

---

<div class="post-metadata">

**Author:** ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)\
**Post date:** [November 2, 2021, 9:33am UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/7 "2021-11-02T09:33:11Z")

</div>

Thank you for an explanation.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 2, 2021, 12:22pm UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/8 "2021-11-02T12:22:06Z")

</div>

I think this will give incorrect results because csvs can have escaped new line characters.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 2, 2021, 1:28pm UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/9 "2021-11-02T13:28:19Z")

</div>

> [@Oscar\_Smith](#):
>
> because csvs can have escaped new line characters.

@Oscar_Smith and @Sukera, thanks for the insights.  
Is this a common/possible situation when working with CSVs containing only numeric data?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 2, 2021, 1:30pm UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/10 "2021-11-02T13:30:17Z")

</div>

This only occurs for CSVs with strings in them.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [November 2, 2021, 1:35pm UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/11 "2021-11-02T13:35:07Z")

</div>

> [@Michal](#):
>
> Thank you, I meant `reusebuffer` . My fault.

It looks unfortunate you didn’t get an error for a misspelled keyword argument though 😕

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 2, 2021, 2:13pm UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/12 "2021-11-02T14:13:41Z")

</div>

Thanks Oscar.  
To better understand this tested a simple familiar Excel scenario with ALT+ENTER to split string labels across two lines in a single cell.  
The OP’s function does provide the correct number of CSV rows = ` 3`, while physical CSV file on disk has ` 5` lines:

**Excel:**  
 ![escaped_sequences_newline_csv_Excel](https://global.discourse-cdn.com/julialang/original/3X/3/4/34c842d2a257cb4ce42594e89aafd6a604041313.png)

**Notepad++:**  
 ![escaped_sequences_newline_csv_notepadplus](https://global.discourse-cdn.com/julialang/original/3X/d/a/dacca863bcaa89cb45a674ecce3a713b1c6c7d30.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 3, 2021, 9:05pm UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/13 "2021-11-03T21:05:58Z")

</div>

> [@Michal](#):
>
> Is it possible to do something like following code?
> 
> ```julia
> usng CSV
> csvRows = CSV.Rows("example.csv")
> countlines(csvRows.buf)
> 
> ```

The Base function `count()` may do what you ask for here:

```julia
csvRows = CSV.Rows(file; header=false)

julia> count(i -> i==i, csvRows)
3

```

_ **NB:** _  
_There should be a better way of writing this part :_`i -> i==i`

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [November 3, 2021, 9:36pm UTC](https://discourse.julialang.org/t/csv-jl-number-of-lines/70785/14 "2021-11-03T21:36:53Z")

</div>

> [@rafael.guerra](#):
>
> _There should be a better way of writing this part :_ `i -> i==i`

It’s `_ -> true`, and another alternative `Returns(true)` will be available when Julia 1.7 gets released.
