Home>

I want to parse a TSV-separated file, TSV, but it doesn't work.
It looks like Ruby doesn't recognize the tab itself ...

Environment

$ruby ​​-v
ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]

dev.csv

HD \ s? 598, HD 598
DCE \ s? 120, DCE 120

dev.tsv

HD \ s? 598 HD 598
DCE \ s? 120 DCE 120

script.rb

Encoding.default_external = 'UTF-8'
require 'csv'
CSV.foreach ('dev.tsv', {: col_sep =>"\ t"}) do | fg |
  puts "[# {fg [0]}] # {fg [1]}: # {fg [2]}"
end
# require 'csv'
CSV.foreach ('dev.csv') do | fg |
  puts "[# {fg [0]}] # {fg [1]}: # {fg [2]}"
end
# Execution result
# =>[HD \ s? 598 HD 598]:
# =>[DCE \ s? 120 DCE 120]:
# =>[HD \ s? 598] HD 598:
# =>[DCE \ s? 120] DCE 120:

Is the tab recognized in the first place? I thought, but it looks like I still don't recognize it ...

text = File.open (File.expand_path ("../ dev.tsv", __FILE__)) .read
if text = ~/\ t /;puts "y";end
# =>No output
# If a tab is included, "y" should be output.

I don't know the cause ...