02-01-2024 03:56 PM
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?
Solved! Go to Solution.
02-01-2024 05:17 PM - edited 02-01-2024 05:18 PM
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")
02-01-2024 05:17 PM - edited 02-01-2024 05:18 PM
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")
02-01-2024 07:22 PM
Thanks Gary! Will give this a try and report back.
02-02-2024 11:17 AM
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!