csvreader encoding options

17 views
Skip to first unread message

Steven Jeffries

unread,
Nov 9, 2018, 2:07:32 AM11/9/18
to wwwmake
Is there a way to pass encoding options to csvreader? I keep getting "invalid byte sequence in UTF-8" when trying to read a CSV file.

Gerald Bauer

unread,
Nov 9, 2018, 2:14:59 AM11/9/18
to www...@googlegroups.com
Hello,

Thanks for trying the (new) csvreader library.

> Is there a way to pass encoding options to csvreader? I keep getting "invalid byte sequence in UTF-8" when trying to read a CSV file.

By default all files are opened with the (file) mode set to
'r:bom|utf-8' [1]

If you use CsvReader.open (or Csv.open) you can pass in the
mode as a second parameter (it works like File.open because it is
File.open :-).

The recommendation is to (always use utf-8 internally) and
convert your "external" source e.g.:

Csv.open( "windows.csv", "r:windows-1251:utf-8" ).read / each /
etc. or something

Let us know how it goes. Cheers. Prost.

[1] https://github.com/csvreader/csvreader/blob/master/lib/csvreader/reader.rb#L11

Steven Jeffries

unread,
Nov 16, 2018, 5:46:12 PM11/16/18
to wwwmake
I finally got around to testing that out. It works, thanks!

I do have another question, but it's not related to this, so I'll start a new thread.

Thanks again!
Reply all
Reply to author
Forward
0 new messages