Scala java.nio.charset.UnmappableCharacterException:输入长度= 1 [英] Scala java.nio.charset.UnmappableCharacterException: Input length = 1

查看:442
本文介绍了Scala java.nio.charset.UnmappableCharacterException:输入长度= 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了几个标题相似的问题,但似乎无法使用任何问题来解决我的问题。我似乎无法加载我的.csv文件:

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")

返回:


java.nio.charset.UnmappableCharacterException:输入长度= 1

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

所以我尝试了:

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

并得到:


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

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

如果文件不存在,我想UTF-8将无法正常工作。 t为UTF-8格式,所以这很有意义,但我不知道下一步该怎么做。

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.

我设法发现编码是Windows-1252使用:

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

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

但这并没有达到我的预期,这是骗局将文件转换为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")

但是返回了:


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

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

请帮助。

推荐答案

请先尝试将excel文件映射到UTF-8,然后再尝试 val源= io.Source.fromFile( UTF-8, C:/mon_usatotaldat.csv)

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

要映射到UTF-8尝试:

To map to UTF-8 try:


(1)打开一个包含信息的Excel文件(.xls,.xlsx)

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

(2)在Excel中,选择 CSV(逗号分隔)(*。csv)作为文件类型
并保存为该类型。

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

(3)在NOTEPAD(在程序下,然后在开始
菜单中的附件下)中,在记事本中打开保存的.csv文件

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

(4)然后选择->另存为..,在另存为框的底部,
有一个标记为编码的选择框。选择UTF-8(请勿使用
ANSI或丢失所有重音等)。选择UTF-8后,将文件
保存为与原始文件略有不同的文件名。

(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.

此文件为UTF-8,保留所有字符和d口音,例如可以导入到MySQL和其他数据库程序中。

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

参考:使用UTF8编码的Excel到CSV

希望这会有所帮助!

这篇关于Scala java.nio.charset.UnmappableCharacterException:输入长度= 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆