2

i have csv file with strange format

2783¦Larson and Sons
967¦Becker Group
333¦Rolfson LLC

I have tried to do this

CSV.foreach("#{Rails.root}/csv_files/suppliers.csv") do |supplier|
  p supplier[0]
end

but have got a string "2783¦Larson and Sons"

How to separate values? For example will return

supplier[0] #=> "2783"
supplier[1] #=> "Larson and Sons"
1
  • 2
    You can specify the delimiter via CSV.for_each(filename, col_sep: '¦') do .... See CSV.new for available options. Commented Aug 28, 2018 at 11:58

1 Answer 1

1

Why would you expect CSV to know how to handle this weird input? You should explicitly specify the encoding and the column separator.

CSV.read("#{Rails.root}/csv_files/suppliers.csv",
      encoding: Encoding::ISO_8859_1,
      col_sep: "\xC2\xA6".force_encoding(Encoding::ISO_8859_1)) do |supplier|
  puts supplier.inspect
end
#⇒ [["2783", "Larson and Sons"],
#   ["967", "Becker Group"],
#   ["333", "Rolfson LLC"]]
Sign up to request clarification or add additional context in comments.

2 Comments

Why do you expect the CSV file to be ISO 8859-1 encoded? And where does \xC2 come from?
@Stefan I just showed an approach, I assume OP knows better what encoding to use. \xC2 came from reverse engineering :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.