cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Parse CSV with Buried Header

GrailAutomation
Deputy Chef III
Deputy Chef III

I need to download and parse a CSV file. The problem is that the first three lines of the CSV are filled with various irrelevant bits of information that do not belong to the table. Row four of the CSV contains the headers; rows five and below contain the data.

I basically want to use the .drop method, but against a [File Contents] datapill.

The first image attached shows how I am downloading the CSV (represented by the [File Contents] datapill. The second image shows the error I receive when I try to use the .drop method (on the [File Contents] datapill) in the Parse CSV step that follows.

Any advice?

1 ACCEPTED SOLUTION

gary1
Executive Chef III
Executive Chef III

You can only use drop on an array, so first split your CSV on line breaks, then drop, then join it back together with line breaks.

Heads up: if your data contains line breaks, this will also replace them. If that's a problem, there are other ways to do this.

 

## your csv
data = "1,2,3
4,5,6
7,8,9
header,header,header
data,data,data"

## fixer upper
data.split(/[\r\n]+/).drop(3).join("\n")

 

gary1_1-1706836715168.png

 

 

View solution in original post

3 REPLIES 3

gary1
Executive Chef III
Executive Chef III

You can only use drop on an array, so first split your CSV on line breaks, then drop, then join it back together with line breaks.

Heads up: if your data contains line breaks, this will also replace them. If that's a problem, there are other ways to do this.

 

## your csv
data = "1,2,3
4,5,6
7,8,9
header,header,header
data,data,data"

## fixer upper
data.split(/[\r\n]+/).drop(3).join("\n")

 

gary1_1-1706836715168.png

 

 

GrailAutomation
Deputy Chef III
Deputy Chef III

Thanks Gary! Will give this a try and report back.

OK so for the record, my biggest problem in all of this (not mentioned originally because I was unaware) is that I *thought* I was downloading a CSV from Google Drive... but turns out I was downloading a Sheet ๐Ÿ˜…

Thanks Gary for your answer. I had to prepend a .to_s method since the [File contents] data pill is not a string (despite appearing that way in the data tree), then worked like a charm!