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.
File containing data separated by tabs
A file that stores data separated by commas (,)
meibo.txt
john m 18
paul m 20
alice f 15
dabid m 17
jasmin f 17
# 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>
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.
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
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.
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