# Is this an efficient way to read a .csv file row by row?

**URL:** https://discourse.julialang.org/t/is-this-an-efficient-way-to-read-a-csv-file-row-by-row/20056
**Category:** General Usage
**Created:** [January 24, 2019, 7:40pm UTC](https://discourse.julialang.org/t/is-this-an-efficient-way-to-read-a-csv-file-row-by-row/20056 "2019-01-24T19:40:04Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [January 24, 2019, 7:40pm UTC](https://discourse.julialang.org/t/is-this-an-efficient-way-to-read-a-csv-file-row-by-row/20056/1 "2019-01-24T19:40:04Z")

</div>

I have a .csv file and I’d like to read it row by row. The following is part of the code. Do you have better suggestions other than this? I’ve googled this, but the answers I found are quite old.

````
 ```
 df=CSV.read("the path to my .csv file")
 ambulances = Vector{Ambulance}(nrow(df))
 for i in 1:size(df,1)
      # do something to each element of the i-th row 
      # use df[i, 1], df[i,2], df[i,3] ,.... df[i,n] to access each element of the i-th row
end

````

```julia

```

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [January 24, 2019, 7:43pm UTC](https://discourse.julialang.org/t/is-this-an-efficient-way-to-read-a-csv-file-row-by-row/20056/2 "2019-01-24T19:43:30Z")

</div>

See the [CSV.jl docs](http://juliadata.github.io/CSV.jl/stable/). You can create a `CSV.File` object, which you can iterate over row by row.

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [January 24, 2019, 7:46pm UTC](https://discourse.julialang.org/t/is-this-an-efficient-way-to-read-a-csv-file-row-by-row/20056/3 "2019-01-24T19:46:02Z")

</div>

@ExpandingMan, thank u for quick reply. Using `CSV.File` object is quicker?

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [January 24, 2019, 7:49pm UTC](https://discourse.julialang.org/t/is-this-an-efficient-way-to-read-a-csv-file-row-by-row/20056/4 "2019-01-24T19:49:19Z")

</div>

Well, probably yes but in general it wouldn’t need to be.

When you create a `CSV.File` object and iterate over it you will be lazily iterating over the rows, so in other words you’ll only be reading them in as you iterate. You therefore will not have to allocate memory to first read badly formatted data into a `DataFrame`, then fix the format. Instead, you can fix the format of each row as you go along (or whatever it is you’re doing).

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [January 24, 2019, 8:02pm UTC](https://discourse.julialang.org/t/is-this-an-efficient-way-to-read-a-csv-file-row-by-row/20056/5 "2019-01-24T20:02:30Z")

</div>

@ExpandingMan, is there a way to get the total number of rows? Probably not, right? as the rows are iterated over one by one.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [January 24, 2019, 8:05pm UTC](https://discourse.julialang.org/t/is-this-an-efficient-way-to-read-a-csv-file-row-by-row/20056/6 "2019-01-24T20:05:39Z")

</div>

> [@bsnyh](#):
>
> is there a way to get the total number of rows? Probably not, right? as the rows are iterated over one by one.

Right, that’s one of the things that’s so terrible about a CSV: there’s no way of knowing the number of rows until you read the whole thing. That said, reading through the whole thing just to count the number of rows can be done more quickly than copying it all into memory. Of course, in the command line you could do `wc -l`… I’m not sure if `CSV.File` gives a nice way of doing this.

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [January 25, 2019, 11:41am UTC](https://discourse.julialang.org/t/is-this-an-efficient-way-to-read-a-csv-file-row-by-row/20056/7 "2019-01-25T11:41:36Z")

</div>

@ExpandingMan, are there better ways to avoid this? Like other file handling techniques, other file format etc?

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [January 25, 2019, 11:43am UTC](https://discourse.julialang.org/t/is-this-an-efficient-way-to-read-a-csv-file-row-by-row/20056/8 "2019-01-25T11:43:00Z")

</div>

@ExpandingMan, maybe the data (the .csv file) can be saved column wise instead of row wise?

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [January 25, 2019, 2:20pm UTC](https://discourse.julialang.org/t/is-this-an-efficient-way-to-read-a-csv-file-row-by-row/20056/9 "2019-01-25T14:20:34Z")

</div>

I’m a maintainer of [Feather.jl](https://github.com/JuliaData/Feather.jl). That format definitely has its own problems, but if you are just looking for fast easy storage of tabular data it nevertheless is a pretty good option. I use Feather quite frequently.

There is of course also [Parquet.jl](https://github.com/JuliaIO/Parquet.jl) but Parquet is a more complicated format intended for really huge datasets.

---

<div class="post-metadata">

### Author: ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)
#### Post date: [January 27, 2019, 5:34am UTC](https://discourse.julialang.org/t/is-this-an-efficient-way-to-read-a-csv-file-row-by-row/20056/10 "2019-01-27T05:34:51Z")

</div>

I agree w/ @ExpandingMan that Feather.jl is a better overall data file format for tabular data. Do note that you can call `f = CSV.File(file); length(f)` to get the # of rows in a csv file. This is because the `CSV.File` constructor scans the entire file to determine the # of rows upfront. If you happen to know the # of rows before parsing, you can also pass `CSV.File(file; limit=number_of_rows)` and it will speed up the initial file scan a bit (since it knows exactly how many rows to expect). It’s also obviously useful for cases when you only want to read a specific set of rows from a file (in conjunction with the `skipto` argument, which is like an offset into the file you want to start reading from).
