Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Encoding issues while parsing CSV file using Roo gem in rails

    • 0
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 875
    Comment on it

    This is the most common and frustrating issue while parsing a csv file in rails i.e. the encoding issue. So in ruby on rails i found a library which basically detects the Character encoding using ICU (dependency package) and the name of the library is CharlockHolmes. And than we have to add the encoding if it is not already there to parsing it successfully. Below is the code:

    1. contents = File.read(filepath) # filepath is the path of the file
    2. begin
    3. detection = CharlockHolmes::EncodingDetector.detect(contents) # passing the contents for detection
    4. encoding = Encoding.find(detection[:encoding]) # find the encoding from detection object
    5. rescue => error
    6. Rails.logger.error("#{error.class}: #{error.message}\n#{error.backtrace.join("\n")}")
    7. encoding = Encoding::ISO_8859_1 # overwrite if encoding is nil
    8. end
    9.  
    10. Roo::CSV.new(filepath, file_warning: :ignore, csv_options: {encoding: encoding}) # adding encoding parameter so that it will not give error

    Note: You must install libicu using this command if you are using ubuntu (sudo apt-get install libicu-dev)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: