[RUBY] When importing CSV with Rails, it was really easy to use the nkf command

When importing a CSV file with Rails, I implemented it like this.

def read_csv
  begin
    rows = CSV.read(file, encoding: 'UTF-8')
  rescue CSV::MalformedCSVError
    begin
      #csv file is UTF-Shift if not 8_Read in JIS
      rows = CSV.read(file, encoding: 'Shift_JIS')
    rescue CSV::MalformedCSVError, Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError
        #csv file is UTF-8 ・ Shift_If it is not JIS
        @error = 'File encoding is UTF-8 or Shift_It is not JIS.'
    end
  end
  rows&.shift #Remove header
  rows
end

problem

There are a lot of exception handling and it is troublesome, and there are only two character codes that can be imported in the first place ...

solution

Apparently anything can be converted using a module called nkf ... https://docs.ruby-lang.org/ja/latest/class/NKF.html

--Install nkf on your PC

brew install nkf
nkf -w --overwrite path of the file you want to convert

If you use this command, you can convert it to a good feeling.

--Try embedding this command in Rails.

def read_csv(file)
  system("nkf -w --overwrite #{file.path}")
  rows = CSV.read(file)
  rows&.shift #Remove the header on the first line
  rows
end

The code is clean.

important point

Please note that you cannot use the nkf command unless you install nkf with the brew install nkf command etc. in advance.

Recommended Posts

When importing CSV with Rails, it was really easy to use the nkf command
Since the du command used when the capacity is full is difficult to use, I tried wrapping it with ruby
Why can I use the rails command installed with gem? ??
What to do if you can't use the rails command
What I was addicted to when implementing google authentication with rails
When a beginner makes a personal app with rails, the procedure to bring it to the starting point anyway.
Is it easy for the user to use when implementing general-purpose functions? Let's be aware of
When you want to add a string type column with a limited length with the `rails generate migration` command
[Rails] I was addicted to the nginx settings when using Action Cable.
Problems I was addicted to when building the digdag environment with docker
[Rails] How to use rails console with docker
Error when trying to use heroku command
Make Firebase Analytics simple and easy to use with the Singleton and Builder patterns
[Rails] When transitioning to a page with link_to, move to the specified location on the page
When I tried to use a Wacom tablet with ubuntu 20.04, I didn't recognize it.
[Rails] When I use form_with, the screen freezes! ??
When you want to use the method outside
Super easy way to use enum with JSP
Try to summarize the common layout with rails
Easy to display hello world with Rails + Docker
How to delete the tweet associated with the user when you delete it at the same time
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
What I was addicted to when trying to properly openAPI/Swagger documentation with Rails + Grape + Grape Swagger
A warning is displayed when trying to use a huge integer with the special variables $ 1, $ 2, $ 3 ...
Summary of how to use the proxy set in IE when connecting with Java
Limit files created with the rails g controller command
[Rails] I don't know how to use the model ...
Addicted to the webpacker that comes standard with Rails 6
When the server does not start with rails s
After posting an article with Rails Simple Calendar, I want to reflect it in the calendar.
When I tried to scroll automatically with JScrollBar, the event handler was drawn only once.
[It takes 3 minutes] When I tried to install VS Code on Ubuntu 18.04, it was unexpectedly easy.
When I personally developed with Rails, it was a painful story that Rails was hit very much
When I tried to run Azure Kinect DK with Docker, it was blocked by EULA
[Rails] What to do when the view collapses when a message is displayed with the errors method
Session was a cookie designed to be erased when the browser was closed and was a method for exchanging it: Rails Tutorial Note-What is a Rails Session?