CSV 3.1.6 ããªãªã¼ã¹ããã¾ããã
CSV 3.1.6 ã«ã¯ãCSV.open
ã«ä¸æ£ãªãã¤ããç½®ææåã§ç½®ãæãããªãã·ã§ã³ã追å ããã®ã«éã£ãããããåãè¾¼ã¾ãã¦ããã®ã§ãã®ç´¹ä»ã§ãã
ã¦ã¼ã¹ã±ã¼ã¹ã¨ãã¦ãMS Excel åãã®ã¨ã³ã³ã¼ãã£ã³ã° (æå㪠CP932
) ã«å¤æã㦠CSV åºåããã«éãã¦ãä¾ãã° Rails ã¢ããªã±ã¼ã·ã§ã³ã®ããªãã¼ã·ã§ã³ä¸è¶³ãªã©ã§å«ã¾ãã¦ãã¾ã£ãä¸æ£ãªãã¤ãããã³ããªã³ã°ãããã±ã¼ã¹ã«æ´»ç¨ã§ãããªãã·ã§ã³ã§ãã
CSV.open(..., invalid: :replace)
æå®ãã¦ããã¨ã³ã³ã¼ãã£ã³ã° (ããã§ã¯ CP932
) ã¸å¤æã§ããªããã¤ããå«ã¾ãã¦ããå ´åãä¸æ£ãªãã¤ããç½®ææåã§ç½®ãæããã¨ãã:invalid => :replace
ãªãã·ã§ã³ã File.open
ã«ããã¾ããCSV 3.1.5 ã¾ã§ã¯ CSV.open
ã§ã¯åãä»ããªããªãã·ã§ã³ã ã£ããããä¾ãã°ä»¥ä¸ã®ããã«æå®ããå¿
è¦ãããã¾ããã
File.open(filename, 'w', encoding: Encoding::CP932, invalid: :replace, undef: :replace) do |file| csv = CSV.new(file, encoding: Encoding::CP932) csv << ... csv.close end
CSV 3.1.6 ãã CSV.open
㧠:invalud
ãªãã·ã§ã³ãåãä»ããããã«ãªã£ãããã以ä¸ã®ããã«æ¸ããã¨ãã§ãã¾ãã
CSV.open(filename, 'w', encoding: Encoding::CP932, invalid: :replace) do |csv| csv << ... end
対å¿ãããããã¯ä»¥ä¸ã§ãã
ã¾ããEncoding::InvalidByteSequenceError
ãèµ·ãããããªã¨ã³ã³ã¼ãã£ã³ã°ã«å¯¾ã㦠invalid: :replace
ãªãã·ã§ã³æå®ããå ´åãæå¾
ã«åã㦠ArgumentError
ãèµ·ãã以ä¸ã®ãããªã³ã¼ãã®åé¡ã«ã¤ãã¦ã解æ¶ããã¦ãã¾ãã
require 'csv' filename = 'foo.csv' File.open(filename, 'w', encoding: Encoding::CP932, invalid: :replace) do |file| CSV.open(filename, encoding: Encoding::CP932) do |rows| rows << ["\x82\xa0"] end end
ããã¯ä¸è¨ã®ãããªã³ã¼ããæ¸ãã¦ããã¨ãã¦ã CSV 3.1.5 ã¾ã§ã¯ä»¥ä¸ã®ã¨ã©ã¼ã«ãªã£ã¦ãããã®ã§ãã
% ruby /tmp/csv.rb Traceback (most recent call last): 10: from /tmp/csv.rb:5:in `<main>' 9: from /tmp/csv.rb:5:in `open' 8: from /tmp/csv.rb:6:in `block in <main>' 7: from /Users/koic/.rbenv/versions/2.7.1/lib/ruby/2.7.0/csv.rb:658:in `open' 6: from /tmp/csv.rb:7:in `block (2 levels) in <main>' 5: from /Users/koic/.rbenv/versions/2.7.1/lib/ruby/2.7.0/csv.rb:1230:in `<<' 4: from /Users/koic/.rbenv/versions/2.7.1/lib/ruby/2.7.0/csv/writer.rb:46:in `<<' 3: from /Users/koic/.rbenv/versions/2.7.1/lib/ruby/2.7.0/csv/writer.rb:46:in `collect' 2: from /Users/koic/.rbenv/versions/2.7.1/lib/ruby/2.7.0/csv/writer.rb:47:in `block in <<' 1: from /Users/koic/.rbenv/versions/2.7.1/lib/ruby/2.7.0/csv/writer.rb:159:in `quote' /Users/koic/.rbenv/versions/2.7.1/lib/ruby/2.7.0/csv/writer.rb:159:in `match?': invalid byte sequence in UTF-8 (ArgumentError)
対å¿ãããããã¯ä»¥ä¸ã§ãã
CSV.open(..., undef: :replace)
ãã¡ãã CSV 3.1.6 ã§ä»¥ä¸ã®ããã« File.open
ã® undef: :replace
ãªãã·ã§ã³ã CSV.open
ã«æå®ãããã¨ãã§ããããã«ãªã£ãã®ã§ãç°¡æ½ã«å¯¾å¿ã§ããããã«ãªãã¾ããã
CSV.open(filename, 'w', encoding: Encoding::CP932, undef: :replace) do |csv| csv << ... end
対å¿ãããããã¯ä»¥ä¸ã§ãã
CSV gem 㯠Gemify ããã¦ãããããRuby ã®ã¢ãããã¼ãã¨ç¬ç«ã㦠bundle update
ã§ãã¾ãã詳ããã¯ä»¥ä¸ã®ã¨ã³ããªãåç
§ãã¦ãã ããã
ruby/csv ã«ããããéã£ãéã«ã¹ãã¼ãã£ã¼ãªã¬ãã¥ã¼ããã¦ããã ããé è¤ãããããã¨ããããã¾ããã