在Scala中解析分离的文本文件

在Scala中解析分离的文本文件,scala,parsing,Scala,Parsing,当我试图解析文件中200k行的txt文件时,出现以下错误: java.nio.charset.UnmappableCharacterException:输入长度=1 在我收到错误消息后,我的程序中断: val bufferedSource = io.Source.fromFile( path) for (line <- bufferedSource.getLines.drop(1)) { line.split('|').toList.drop(1) } val buffe

当我试图解析文件中200k行的txt文件时,出现以下错误:

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

在我收到错误消息后,我的程序中断:

val bufferedSource = io.Source.fromFile( path)
for (line <- bufferedSource.getLines.drop(1)) {
    line.split('|').toList.drop(1)
    }
val bufferedSource=io.Source.fromFile(路径)

对于(line不幸的是,您必须自己处理编码问题。 以下两种编码中的一种通常对我有效:

val bufferedSource = io.Source.fromFile( path, enc = Codec.UTF8.name)
for (line <- bufferedSource.getLines.drop(1)) {
    line.split('|').toList.drop(1)
    }

可能重复此错误不在第一行。我在200k行有错误。这似乎仍然是一个字符编码问题。您需要处理非ascii字符或确保文件只有acii值。我的解决方案是:
import scala.io.Codec implicit val Codec=Codec(“cp1251”)Codec.onMalformedInput(CodingErrorAction.REPLACE)codec.onUnmappableCharacter(CodingErrorAction.REPLACE)
val bufferedSource = io.Source.fromFile( path, enc = Codec.ISO8859.name)