# Revisited : Loading first few lines from data file

**URL:** <https://discourse.julialang.org/t/revisited-loading-first-few-lines-from-data-file/41390>\
**Category:** New to Julia\
**Created:** [June 14, 2020, 4:49pm UTC](https://discourse.julialang.org/t/revisited-loading-first-few-lines-from-data-file/41390 "2020-06-14T16:49:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [June 14, 2020, 4:49pm UTC](https://discourse.julialang.org/t/revisited-loading-first-few-lines-from-data-file/41390/1 "2020-06-14T16:49:01Z")

</div>

I am revisiting a question I had asked [earlier](https://discourse.julialang.org/t/loading-first-few-lines-from-data-file/21412), but none of the solutions work on my `1.1.0` version Julia. I have a data file with 100 rows of data of the form

```julia
1 2
3 4
5 6.1
...

```

I want to load the first few lines, say 10 of them, as a 10X2 array. Three solutions suggested to me are now yielding errors. I am at a loss on how to proceed. I do not want to read the entire file and then truncate the number of rows. I am listing below three methods that were suggested to me and also the error messages they create.

Method I : using **head -n**

```julia
julia> x = open(`head -n$100 $input_file`) do io
                  readdlm(io);
                         end;
ERROR: IOError: could not spawn `head -n100 Rot_golden_Complex_N=1E6_DeltaT=1E-2.txt`: no such file or directory (ENOENT)
Stacktrace:
 [1] _spawn_primitive(::String, ::Cmd, ::Array{Any,1}) at .\process.jl:400
 [2] setup_stdios(::getfield(Base, Symbol("##505#506")){Cmd}, ::Array{Any,1}) at .\process.jl:413
 [3] _spawn at .\process.jl:412 [inlined]
 [4] #open#514(::Bool, ::Bool, ::Function, ::Cmd, ::Base.DevNull) at .\process.jl:657
 [5] open at .\process.jl:648 [inlined] (repeats 2 times)
 [6] open(::getfield(Main, Symbol("##275#276")), ::Cmd) at .\process.jl:678
 [7] top-level scope at none:0

```

Method II : using \*_open( readlines …_

```julia
julia> julia> x = open(readlines, `head -n10 $(input_file)`)
ERROR: IOError: could not spawn `head -n10 Rot_golden_Complex_N=1E6_DeltaT=1E-2.txt`: no such file or directory (ENOENT)
Stacktrace:
 [1] _spawn_primitive(::String, ::Cmd, ::Array{Any,1}) at .\process.jl:400
 [2] setup_stdios(::getfield(Base, Symbol("##505#506")){Cmd}, ::Array{Any,1}) at .\process.jl:413
 [3] _spawn at .\process.jl:412 [inlined]
 [4] #open#514(::Bool, ::Bool, ::Function, ::Cmd, ::Base.DevNull) at .\process.jl:657
 [5] open at .\process.jl:648 [inlined] (repeats 2 times)
 [6] open(::typeof(readlines), ::Cmd) at .\process.jl:678
 [7] top-level scope at none:0

```

Method III : using **CSV** package

```julia
julia> CSV.read("input_file", delim = ' ', rows = 10)
ERROR: MethodError: no method matching CSV.File(::String; delim=' ', rows=10)
Closest candidates are:
  CSV.File(::Any; header, normalizenames, datarow, skipto, footerskip, limit, transpose, comment, use_mmap, ignoreemptylines, threaded, select, drop, missingstrings, missingstring, delim, ignorerepeated, quotechar, openquotechar, closequotechar, escapechar, dateformat, decimal, truestrings, falsestrings, type, types, typemap, categorical, pool, strict, silencewarnings, debug, parsingdebug, allowmissing) at C:\Users\iamsu\.julia\packages\CSV\76SRf\src\CSV.jl:262 got unsupported keyword argument "rows"
Stacktrace:
 [1] kwerr(::NamedTuple{(:delim, :rows),Tuple{Char,Int64}}, ::Type, ::String) at .\error.jl:125
 [2] (::getfield(Core, Symbol("#kw#Type")))(::NamedTuple{(:delim, :rows),Tuple{Char,Int64}}, ::Type{CSV.File}, ::String) at .\none:0
 [3] #read#68(::Bool, ::Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol},NamedTuple{(:delim, :rows),Tuple{Char,Int64}}}, ::Function, ::String) at C:\Users\iamsu\.julia\packages\CSV\76SRf\src\CSV.jl:1156
 [4] (::getfield(CSV, Symbol("#kw##read")))(::NamedTuple{(:delim, :rows),Tuple{Char,Int64}}, ::typeof(CSV.read), ::String) at .\none:0
 [5] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [June 14, 2020, 7:14pm UTC](https://discourse.julialang.org/t/revisited-loading-first-few-lines-from-data-file/41390/2 "2020-06-14T19:14:04Z")

</div>

Which operating system are you using? head is a Linux/Mac utility . It does not work on windows

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [June 14, 2020, 7:47pm UTC](https://discourse.julialang.org/t/revisited-loading-first-few-lines-from-data-file/41390/3 "2020-06-14T19:47:00Z")

</div>

Oh I see. The problem did arise on my Windows computer. It works fine on my linux computer at work, but we are on lockdown ☹  
I am a bit surprised to hear that the syntax is OS dependent. Could you suggest a way around ?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 15, 2020, 8:01am UTC](https://discourse.julialang.org/t/revisited-loading-first-few-lines-from-data-file/41390/4 "2020-06-15T08:01:20Z")

</div>

Given that you’re on a relatively outdated Julia version (I would recommend moving to 1.4.2) you might also be on an old CSV version, so I can’t promise that this works, but when using CSV you should use the `limit` keyword, not `rows`. See the docs [here](https://juliadata.github.io/CSV.jl/stable/#CSV.File), you probably want:

```julia
DataFrame(CSV.File("input_file", delim = ' ', limit = 10))

```

You might also want to consider `header=false` as additional kwarg given that you say you want to get a 10x2 array from the first 10 rows (implying there’s no header to be read in).

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [June 15, 2020, 1:03pm UTC](https://discourse.julialang.org/t/revisited-loading-first-few-lines-from-data-file/41390/5 "2020-06-15T13:03:13Z")

</div>

Thanks, I am going to try it out. The documentation of CSV.jl [here](https://juliadata.github.io/CSV.jl/v0.1.1/) says that `rows` is a valid optional argument.  
Also, is there a command to update my Julia version ? This question was posted [here](https://discourse.julialang.org/t/how-to-upgrade-julia-to-a-new-release/17381) but no answer was provided.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 15, 2020, 1:13pm UTC](https://discourse.julialang.org/t/revisited-loading-first-few-lines-from-data-file/41390/6 "2020-06-15T13:13:17Z")

</div>

Note that the docs you are linking to are for CSV version 0.1.1, released in 2016 - not your fault, but you have to be careful when googling for docs, as Google will randomly serve up some ancient version of the documentation. Make sure that you see `stable` somewhere in the URL ideally.

If you’re on Arch Linux you could use the AUR [julia-bin](https://aur.archlinux.org/packages/julia-bin/) repo which will automatically update your Julia version, other than that I don’t think there are ways to do this directly, so you’d just have to download the latest release from [Download Julia](http://julialang.org/downloads).

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [June 15, 2020, 1:18pm UTC](https://discourse.julialang.org/t/revisited-loading-first-few-lines-from-data-file/41390/7 "2020-06-15T13:18:32Z")

</div>

I see. Henceforth I will look for the keyword `stable` in the url. Also, your suggestion for using the CSV worked, thank you. It looks to me like things are much easier to do on Linux. I will use your method for the moment till my office reopens.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 15, 2020, 1:48pm UTC](https://discourse.julialang.org/t/revisited-loading-first-few-lines-from-data-file/41390/8 "2020-06-15T13:48:29Z")

</div>

FWIW I use Windows at work and it isn’t particularly burdensome to update Julia - just download the `.exe` from the website, click and install, and if desired copy your `1.2` environments folder and rename it `1.4` to move all your installed packages to the new version.

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [June 15, 2020, 4:04pm UTC](https://discourse.julialang.org/t/revisited-loading-first-few-lines-from-data-file/41390/9 "2020-06-15T16:04:47Z")

</div>

@nilshg has got the correct answer for you

To explain what is happening, this line says ‘run the os command called head -n$100 $input\_file’  
julia\> x = open(`head -n$100 $input_file`) do io

Look closely - the quotes are ` and are commonly called backticks.  
[https://docs.julialang.org/en/v1/manual/running-external-programs/](https://docs.julialang.org/en/v1/manual/running-external-programs/)

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [June 15, 2020, 4:08pm UTC](https://discourse.julialang.org/t/revisited-loading-first-few-lines-from-data-file/41390/10 "2020-06-15T16:08:40Z")

</div>

A search says that this comman dis equivalent to head on the Windows os

gc -head 10 filename

I woudl really advise to use the method @nilshg suggests. Writing code fro a particular OS is inflexible.
