0
0

More than 1 year has passed since last update.

#Ruby で SJIS のCSVファイルを書き込む、読み込む

Last updated at Posted at 2020-01-26

encodingフラグを使って色々とやる
stringに対しての直接のencodeは必要なさそう

File.write('tmp/sjis.csv', "あ,い,う\nえ,お,か", encoding: Encoding::SJIS)
#  => 17

File.read('tmp/sjis.csv', encoding: Encoding::SJIS)
# => "あ,い,う\nえ,お,か"

CSV.read('tmp/sjis.csv', encoding: Encoding::SJIS)
# => [["あ", "い", "う"], ["え", "お", "か"]]


CSV.open('tmp/sjis2.csv', 'w', encoding: Encoding::SJIS) do |row|
  row << ['か', 'き', 'く']
  row << ['け', 'こ', 'さ']
end
# => <#CSV io_type:File io_path:"tmp/sjis2.csv" encoding:UTF-8 lineno:2 col_sep:"," row_sep:"\n" quote_char:"\"">

File.read('tmp/sjis2.csv', encoding: Encoding::SJIS)
# => "か,き,く\nけ,こ,さ\n"

CSV.read('tmp/sjis2.csv', encoding: Encoding::SJIS)
# => [["か", "き", "く"], ["け", "こ", "さ"]]

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0