Project

General

Profile

Bug #4603 » csv_test.rb

nobuoka (yu nobuoka), 04/24/2011 03:33 PM

 
# coding: UTF-8

require 'csv'
Encoding.default_external #=> #<Encoding:UTF-8>

# create a csv file for test
File.open( 'test.csv', 'w' ) do |f|
f << '"test of encoding",124' << "\n"
f << '"日本語: non ascii (ja)",400' << "\n"
end

# CSV::read
c = CSV.read( 'test.csv' )
c[0][0].encoding #=> #<Encoding:ASCII-8BIT>
# expected value is #<Encoding:UTF-8> (Encoding.default_external)
c[1][0].encoding #=> #<Encoding:ASCII-8BIT>
# expected value is #<Encoding:UTF-8> (Encoding.default_external)

# CSV::foreach
CSV.foreach( 'test.csv' ) do |l|
puts "#{l} : #{l[0].encoding}"
end

# CSV::open
c = CSV.open( 'test.csv' )
dat = c.read()
dat[1][0].encoding #=> #<Encoding:ASCII-8BIT>
# expected value is #<Encoding:UTF-8> (Encoding.default_external)
c.close()
    (1-1/1)