1

I've found several questions with similar titles, but couldn't seem to use any to resolve my issue. I Can't seem to load my .csv file:

val source = io.Source.fromFile("C:/mon_usatotaldat.csv")

Returns:

java.nio.charset.UnmappableCharacterException: Input length = 1

So I tried:

val source = io.Source.fromFile("UTF-8", "C:/mon_usatotaldat.csv")

and got:

java.nio.charset.IllegalCharsetNameException: C:/mon_usatotaldat.csv

I guess UTF-8 wouldn't work, if the file isn't in UTF-8 format, so that makes sense, but I don't know what to do next.

I've managed to discover the encoding is windows-1252 using:

val source = io.Source.fromFile("C:/mon_usatotaldat.csv").codec.decodingReplaceWith("UTF-8")

But this didn't do what I had expected, which was convert the file to UTF-8. I have no Idea how to work with it.

Another thing I've tried was:

val source = io.Source.fromFile("windows-1252","C:/mon_usatotaldat.csv")

But that returned:

java.nio.charset.IllegalCharsetNameException: C:/mon_usatotaldat.csv

Please help. Thanks in advance.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
Laserbeak43
  • 595
  • 2
  • 8
  • 21

2 Answers2

3

Try mapping your excel file to UTF-8 first and then try val source = io.Source.fromFile("UTF-8", "C:/mon_usatotaldat.csv")

To map to UTF-8 try:

(1) Open an Excel file where you have the info (.xls, .xlsx)

(2) In Excel, choose "CSV (Comma Delimited) (*.csv) as the file type and save as that type.

(3) In NOTEPAD (found under "Programs" and then Accessories in Start menu), open the saved .csv file in Notepad

(4) Then choose -> Save As..and at the bottom of the "save as" box, there is a select box labelled as "Encoding". Select UTF-8 (do NOT use ANSI or you lose all accents etc). After selecting UTF-8, then save the file to a slightly different file name from the original.

This file is in UTF-8 and retains all characters and accents and can be imported, for example, into MySQL and other database programs.

Reference: Excel to CSV with UTF8 encoding

Hope this helps!

Community
  • 1
  • 1
Rana
  • 1,675
  • 3
  • 25
  • 51
  • I tried opening it up in notepad directly, since I did try to see if saving it as a comma delimited file would work last night, and I got the same error. @ug_'s suggestion did work for me on the file I saved in UTF-8 format like you've suggested. No luck on the original file. Thanks! – Laserbeak43 Feb 02 '16 at 04:01
  • Not a problem. Cheers – Rana Feb 02 '16 at 04:58
  • 2
    This way round: 'val source = io.Source.fromFile("C:/mon_usatotaldat.csv", "UTF-8")' – sungiant Nov 27 '16 at 08:33
1

Set up an InputStreamReader to correctly read windows-1252. Don't bother with intermediate UTF-8.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • @ug_ has given me the answer in his comment, I had the parameters backwards. :P But I am curious, what do you mean by "Don't bother with intermediate UTF-8"? Does that have something to do with my apparent skill level? :) – Laserbeak43 Feb 02 '16 at 03:59