How to handle TSV files and CSV files in Ruby

Introduction

In this article, I have summarized the basic knowledge of handling TSV files and CSV files in Ruby like my memo. Before TSV and CSV, I would like to check the File class together.

What is a TSV file?

File containing data separated by tabs

What is a CSV file?

A file that stores data separated by commas (,)

In this article, the following TSV files are used as examples.

 meibo.txt
john	m	18
paul	m	20
alice	f	15
dabid	m	17
jasmin	f	17

About the File class

Open file

# How to write 1
 File.open("meibo.txt") do |file|
 processing
end

# How to write 2
file = File.open("meibo.txt")
 processing
file.close

Writing 1 will close the file without permission even if you open it, but writing 2 will keep the file open unless you use the close method. So, I personally like Writing 1.

 File.open("meibo.txt") do |file|
  puts.file
end

For example, if you do the above, you will get the standard output of the File object as follows.

<File:0x00007fba4497ea68>

Read the file

The opened file will be read.

 File.open("meibo.txt") do |file|
  p file.read
end

Then, the standard output will be as follows.

"john\tm\t18\npaul\tm\t20\nalice\tf\t15\ndabid\tm\t17\njasmin\tf\t17\n"

You can read it suddenly.

file = File.read("meibo.txt")
p file

=>
"john\tm\t18\npaul\tm\t20\nalice\tf\t15\ndabid\tm\t17\njasmin\tf\t17\n"

If you want to read line by line, open the file once, read it line by line in the processing part, and insert your favorite processing as shown below.

 File.open("meibo.txt") do |file|
   file.each do |line|
 processing
  end
end

# For example

 File.open("meibo.txt") do |file|
   file.each do |line|
    p "#{line.chomp}\tfoo"
  end
end

=>
"john\tm\t18\tfoo"
"paul\tm\t20\tfoo"
"alice\tf\t15\tfoo"
"dabid\tm\t17\tfoo"
"jasmin\tf\t17\tfoo"

The above is how to handle it in the File class, but since the output is a string of beads, I think it is a little difficult to handle. Therefore, you can make the data easier to handle by using the CSV class.

About CSV class

I think that it is originally a class for handling CSV files, but if you use the col_sep option (separates by the specified character string), TSV files can also be handled by the CVS class. When using the CSV class, add require" csv " at the beginning of the line.

require "csv"

# If you use the open method
 CSV.open("meibo.txt", col_sep: "\t") do |tsv|
  p tsv.read
end

# If you don't use the open method
tsv = CSV.read("meibo.txt", col_sep: "\t")
p tsv

=>
[["john", "m", "18"], ["paul", "m", "20"], ["alice", "f", "15"], ["dabid", "m", "17"], ["jasmin", "f", "17"]]

When read using the CSV method, the output result is returned in an array. This makes it easier to handle as data. If you want to read line by line, use the following foreach method.

require "csv"

 CSV.foreach("meibo.txt", col_sep: "\t") do |line|
 processing
end

# For example
 CSV.foreach("meibo.txt", col_sep: "\t") do |line|
  p line
end

=>
["john", "m", "18"]
["paul", "m", "20"]
["alice", "f", "15"]
["dabid", "m", "17"]
["jasmin", "f", "17"]

This allows you to work on the array for each row.

By the way, of course, you can also open a new CSV file and write to it.

require "csv"

 CSV.open("meibo.csv","w") do |line| #"w"Is in write mode
 line << ["michel","m",16]
end

 meibo.csv
michel,m,16

Convert TSV to CSV

Finally, you can convert TSV files to CSV files using the syntax introduced so far. Suddenly the conclusion is as follows.

require "csv"

 CSV.open('meibo.csv', 'w') do |csv|
   CSV.foreach("meibo.txt", col_sep: "\t") do |line|
   csv << line
 end
end

First, prepare a CSV file in write mode. In that CSV file, foreach is used to store each line of the TSV file in CSV one line at a time. You can do the opposite for CSV to TSV.

at the end

I didn't understand the File class a little at first, but I was able to organize it in my head while writing this article, so I will continue to use it.

Recommended Posts

How to handle TSV files and CSV files in Ruby
Converting TSV files to CSV files (with BOM) in Ruby
Put CSV files containing "'" and "" "in MySQL in Ruby 2.3
How to iterate infinitely in Ruby
How to install Bootstrap in Ruby
Handle CSV files uploaded to GCS
Differences in how to handle strings between Java and Perl
How to get date data in Ruby
[Java] How to output and write files!
How to get and add data from Firebase Firestore in Ruby
[Easy] How to upgrade Ruby and bundler
Ruby: CSV :: How to use Table Note
Convert JSON to TSV and TSV to JSON with Ruby
[Ruby] How to convert CSV file to Yaml (Yml)
[Ruby] How to use gsub method and sub method
How to handle optional in Protocol Buffers (proto3)
[Ruby on Rails] How to install Bootstrap in Rails
How to build parquet-tools and merge Parquet files
How to build the simplest blockchain in Ruby
Ruby How to convert between uppercase and lowercase
How to implement Pagination in GraphQL (for ruby)
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
How to ZIP a JAVA CSV file and manage it in a Byte array
[Ruby] How to use standard output in conditional branching
How to input / output IBM mainframe files in Java?
[Ruby On Rails] How to reset DB in Heroku
How to launch another command in a Ruby program
How to Syntax Highlight files like Dockerfile.production in Pycharm
[Ruby] How to get the tens place and the ones place
(Ruby on Rails6) How to create models and tables
How to launch Swagger UI and Swagger Editor in Docker
How to resolve SSL_connect error in PayPal Ruby SDK
How to handle uploaded images
How to use Ruby return
[Ruby] How to comment out
Ruby: How to use cookies
[Ruby] How to write blocks
How to handle an instance
How to rollback migration files
How to specify character code and line feed code in JAXB
How to separate words in names in classes, methods, and variables
[Forge] How to register your own Entity and Entity Render in 1.13.2
How to change a string in an array to a number in Ruby
[Rails] How to define macros in Rspec and standardize processing
How to retrieve the hash value in an array in Ruby
How to call and use API in Java (Spring Boot)
How to display a graph in Ruby on Rails (LazyHighChart)
How to develop and register a Sota app in Java
How to install PHP 7.4 and SQL Server drivers in CentOS 7.7
How to check the extension and size of uploaded files
[Xcode] How to arrange Xcode and Simulator screens in full screen
How to include PKCE Code_Verifier and Code_Challenge in JMeter requests
How to dynamically switch between FIN and RST in Netty
How to deal with different versions of rbenv and Ruby
Write DiscordBot to Spreadsheets Write in Ruby and run with Docker
[Ruby] How to batch convert strings in an array to numbers
How to use Lombok in Spring
How to use StringBurrer and Arrays.toString.
How to find May'n in XPath
How to hide scrollbars in WebView
How to run JUnit in Eclipse