# Reading gz'ed CSV does not work - length of provided header doesn't match the number of columns

**URL:** https://discourse.julialang.org/t/reading-gzed-csv-does-not-work-length-of-provided-header-doesnt-match-the-number-of-columns/27106
**Category:** General Usage
**Tags:** csv
**Created:** [August 2, 2019, 9:50am UTC](https://discourse.julialang.org/t/reading-gzed-csv-does-not-work-length-of-provided-header-doesnt-match-the-number-of-columns/27106 "2019-08-02T09:50:20Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)
#### Post date: [August 2, 2019, 9:50am UTC](https://discourse.julialang.org/t/reading-gzed-csv-does-not-work-length-of-provided-header-doesnt-match-the-number-of-columns/27106/1 "2019-08-02T09:50:20Z")

</div>

I have a trivial program working on a simple gz’ed CSV (available [here](http://www.hmmueller.de/20190802/Perf_Health_MSSQL%24PTSQL_Buffer%20Manager_Page%20life%20expectancy_21-8h.csv.gz)):

```julia
using CSV
df = CSV.File("Perf_Health_MSSQL\$PTSQL_Buffer Manager_Page life expectancy_21-8h.csv.gz",
     header=[:domain, :host, :feature, :oid, :largeversion, :clientid, 
        :from, :to, :aggrlevel, :firstocc, :lastocc, :livesuntil, 
        :ct, :sum, :min, :max, :g_lower, :g_upper, :g_ct, :g_sum],
    delim='|')

```

This fails with

> ArgumentError: The length of provided header (20) doesn’t match the number of columns at row 1 (5).

Manually unpacking the file and reading it works perfectly. Can anyone tell me what the problem is, and maybe how to solve it?  
Especially, I dont understand why there would be 5 rows in the first row …

// Edit: Suspecting that the delim does not work with a gz’ed file (but why??), I “analyzed” the first line with

```julia
using StatsBase
filter((k,v)->v==4, countmap(collect("PHARMATECHNIK|STA-WS174|Perf/Health:MSSQL\$PTSQL:Buffer Manager\\Page life expectancy:21-8h|15294|2019.11|STA-WS174|2019-07-29T21:00:00|2019-07-29T21:10:00|2|2019-07-29T21:00:06|2019-07-29T21:09:06|2019-08-03T21:10:00|10|7549.00000|484.00000|1024.00000||||")))

```

There are a few letters that occur 4 times (thus splitting the line into 5 columns) - but none makes much sense as a default delimiter:

```julia
  'P' => 4
  '.' => 4
  'f' => 4
  'A' => 4

```

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [August 2, 2019, 10:27am UTC](https://discourse.julialang.org/t/reading-gzed-csv-does-not-work-length-of-provided-header-doesnt-match-the-number-of-columns/27106/2 "2019-08-02T10:27:24Z")

</div>

Hi,

I rather think that the problem is that you should send an unpacked stream to CSV.jl. You can use [https://github.com/bicycle1885/CodecZlib.jl](https://github.com/bicycle1885/CodecZlib.jl) to do it. Something like:

```julia
using CSV, CodecZlib
io = open(yourfilename)
data = CSV.read(GzipDecompressorStream(io))
close(io)

```

should work

---

<div class="post-metadata">

### Author: ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)
#### Post date: [August 2, 2019, 10:35am UTC](https://discourse.julialang.org/t/reading-gzed-csv-does-not-work-length-of-provided-header-doesnt-match-the-number-of-columns/27106/3 "2019-08-02T10:35:31Z")

</div>

> [@bkamins](#):
>
> using CSV, CodecZlib io = open(yourfilename) data = CSV.read(GzipDecompressorStream(io)) close(io)

Yes, thanks, works - and it seems that the .gz reading version actually ignores the delim parameter - because your code (without a delim) also claims that there are 5 columns in the file.

// Edit: … or, ahem, it simply does not support gz reading. I might be confused because CSVFiles claims to support it … But then, the error message is really unhelpful …

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [August 2, 2019, 10:42am UTC](https://discourse.julialang.org/t/reading-gzed-csv-does-not-work-length-of-provided-header-doesnt-match-the-number-of-columns/27106/4 "2019-08-02T10:42:50Z")

</div>

my code uses CSV.jl delimiter autodetection feature (but this is unrelated to decompression). If you still have problems and can share the file I can check.

---

<div class="post-metadata">

### Author: ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)
#### Post date: [August 2, 2019, 10:44am UTC](https://discourse.julialang.org/t/reading-gzed-csv-does-not-work-length-of-provided-header-doesnt-match-the-number-of-columns/27106/5 "2019-08-02T10:44:27Z")

</div>

A link for downloading the data file is in my first posting - but does CSV actually read gz natively (without the Gz…Stream)?? I might have supposed this wrongly …

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [August 2, 2019, 11:50am UTC](https://discourse.julialang.org/t/reading-gzed-csv-does-not-work-length-of-provided-header-doesnt-match-the-number-of-columns/27106/6 "2019-08-02T11:50:26Z")

</div>

IIUC, CSVFiles.jl does but not CSV.jl.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [August 2, 2019, 12:21pm UTC](https://discourse.julialang.org/t/reading-gzed-csv-does-not-work-length-of-provided-header-doesnt-match-the-number-of-columns/27106/7 "2019-08-02T12:21:16Z")

</div>

I have checked and my code works - you have to specify `|` as `delim` kwarg (the reason is that audtodetection puts preference for space and space produces a valid output).

---

<div class="post-metadata">

### Author: ![hmmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmmueller/32/9617_2.png) [@hmmueller](https://discourse.julialang.org/u/hmmueller)
#### Post date: [August 2, 2019, 12:21pm UTC](https://discourse.julialang.org/t/reading-gzed-csv-does-not-work-length-of-provided-header-doesnt-match-the-number-of-columns/27106/8 "2019-08-02T12:21:51Z")

</div>

Thanks - now I understand all of what happens! Muchas gracias to all …
