When reading a csv file with ruby, is it possible to read only the first two lines including the header and not the others?
The number of lines in the csv file is large, and if it is.reador.foreach, it takes time to process because all lines are read.
Specifically, the following CSV data has about 20000 rows,
Since we want to output only "1,20180821,100,35" on the second line, there is no need to read the third and subsequent lines.
No, Time, kakaku, kosuu
When I looked into it, I felt that it was quite so much if I used.map, but I didn't understand.
It would be helpful if you could give me sample code.
(It is not possible to create a separate file for the first and second lines and read it.)
Answer # 1
require "csv" CSV.open ("file.csv") do | csvfile | title = csvfile.gets data = csvfile.gets p title p data end
I want to output only "1,20180821,100,35" on the second line
If you want to output as a string instead of CSV data,
open ("file.csv") do | file | title = file.gets data = file.gets puts title puts data end
Answer # 2
The CSV library can parse a single line of text as CSV using CSV.parse_line.
Also, if you pass Array corresponding to the header line of CSV with: headers, you can use it like Hash with header as key.
If the sample CSV is named foo.csv, it will be as follows.
require 'csv' csv_filename = "foo.csv" File.open (csv_filename) do | f | line1 = f.gets line2 = f.gets header = CSV.parse_line (line1) csv_body = CSV.parse_line (line2, headers: header) p csv_body ["kosuu"] # =>35 end
Answer # 3
require 'csv' row2 = nil row = CSV.foreach ('data.csv', headers: true) do | row | row2 = row break end p row2
- i want to read a csv file with python and save it as excel
- python - read csv file with keras
- python - cannot read csv file
- read csv file and use in list [java]
- ruby - certain columns in csv file are not read
- python - read csv file
- i want to read a csv file in c # and output it in json format
- python - cannot read csv file
- Python read csv file and put the file into a list
- Android read data in XML file
- c # - unable to read wav file
- jupyter import csv file
- How to read JSON configuration file in C # project in vs
- ruby on rails 6 - first argument in form cannot contain nil or be empty cannot be resolved
- reading csv file in python
- ruby on rails 5 - how to read logs and the meaning of errors
- C ++ read data file to array instance
- ruby file in rails: models/concern does not work
- python 3x - typeerror: 'method' object is not subscriptable
- python - you may need to restart the kernel to use updated packages error
- xcode - pod install [!] no `podfile 'found in the project directory
- android studio - unresolved reference comes out in kotlin
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- django - oserror: [winerror 123] the file name, directory name, or volume label syntax is incorrect : '<frozen importlib_boot
- mysql startup failed [error] innodb: the innodb_system data file 'ibdata1' must be writable
- python 3x - import error "cannot import name '_imaging'"