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
- ruby - certain columns in csv file are not read
- i want to read a csv file with python and save it as excel
- python - cannot read csv file
- python - read csv file with keras
- ruby - csv file reading in rails
- python - read csv file
- read csv file and use in list [java]
- i want to read a csv file in c # and output it in json format
- python - the file cannot be read
- i want to read the csv file from s3 of aws with java and read the contents line by line to process
- i want to download a csv file as a template with php
- input/output of csv file with python
- vba - i want to read csv and write to excel
- Python read parquet file on hdfs
- python - when i read the file, it says that there is no key
- read file (css) in wordpress
- read a file in python
- ruby - i can't find the file i made with the command prompt
- ruby on rails - about the error of first argument in form cannot contain nil or be empty
- ruby on rails - rails tutorial rails 60 chapter 1 first, can't install yarn
- 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
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture
- android studio - unresolved reference comes out in kotlin
- mysql startup failed [error] innodb: the innodb_system data file 'ibdata1' must be writable
- django - oserror: [winerror 123] the file name, directory name, or volume label syntax is incorrect : '<frozen importlib_boot
- python - importerror: cannot import name md5 error cannot be resolved