Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you parse just the first line of a CSV file?

How can you parse just the first line of a CSV file? I want to make sure that all of the appropriate columns are provided in the file, but don't want to process the whole file.

like image 892
Tom Rossi Avatar asked Aug 07 '13 20:08

Tom Rossi


People also ask

How do I get the first column of a csv file?

Example: Read Only First Column from CSV File To achieve this, we have to set the usecols argument within the read_csv function to be equal to [1].

How to parse CSV in Node js?

You will use the fs module's createReadStream() method to read the data from the CSV file and create a readable stream. You will then pipe the stream to another stream initialized with the csv-parse module to parse the chunks of data. Once the chunks of data have been parsed, you can log them in the console.

What does CSV parse do?

Comma Separated Values (CSV) is used as a common format to exchange tabular data between spreadsheets and relational databases. In a JavaScript action, you can parse CSV data using the csv library.


1 Answers

A better way of doing this is to simply use the built-in Enumerable support in Ruby's Standard Library CSV parser:

headers = CSV.open('file.csv', 'r') { |csv| csv.first }

The block will result in the file automatically being closed and the call will return an array of the parsed headers.

like image 115
coreyward Avatar answered Sep 28 '22 20:09

coreyward