Ruby 1.9, CSV and encodings

P

Patrick Roemer

Hi,

I'd like to process an ISO-8859-1 encoded .csv file on a UTF-8 system
using Ruby 1.9. The naive approach of using the CSV lib fails with
"invalid mbstring sequence". I don't see where to specify the encoding.
I've even tried the '--encoding' command line switch, but it doesn't
seem to know ISO-8895-1 in all spellings I could come up with. (BTW,
where is command line switch behavior like this documented in detail?)

Currently I'm reading the complete file, enforce the encoding and
process the in-memory string:

file_content = IO.readlines(file_name).join
file_content.force_encoding('ISO-8859-1')
parser = CSV.new(file_content, ...)

But that looks like a rather clumsy hack. What's the "right" way to do it?

Thanks and best regards,
Patrick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top