I am building an app which reads the data from the CSV file and stores those data into the database. I csv file which I get from the 3rd party consists of header in the first row which has to be deleted before pushing those data in to the database. How do I delete the first row pro grammatically and then push only the values or data to the database.I am using ruby 1.9.2 version, so I dont have to use "fastercsv". I am using the standard CSV library.
Kindly help me out
when you do:
CSV.open(filename, :headers => true) do |row|
# it will use the first line as headers. now you can do:
row['my_header_value']
end
You can fetch all the rows at once by using:
arr_of_arrs = CSV.read("path/to/file.csv")
And then you can use arr_of_arrs.drop(1)
to remove header.
Or you can use arr_of_arrs.each_with_index
to skip the first row (header), like this:
arr_of_arrs.each_with_index do |e, i|
next if i == 0
#process each row here
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With