【问题标题】:Parsing separated text file in Scala在 Scala 中解析分离的文本文件
【发布时间】:2017-02-13 06:13:33
【问题描述】:

当我尝试在文件中解析 200k 行的 txt 文件时出现此错误:

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

出现错误后我的程序中断:

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

如果我理解正确,错误是in io.Source.fromFile( path)。 我怎样才能跳过坏行?

【问题讨论】:

  • 我在第一行没有这个错误。我在 200k 行有错误
  • 这似乎仍然是字符编码问题。您要么需要处理非 ascii 字符,要么确保您的文件只有 acii 值。
  • 我的解决方案是:import scala.io.Codec implicit val codec = Codec("cp1251") codec.onMalformedInput(CodingErrorAction.REPLACE) codec.onUnmappableCharacter(CodingErrorAction.REPLACE)

标签: scala parsing


【解决方案1】:

不幸的是,您必须自己处理编码问题。 这两种编码中的一种通常对我有用:

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

val bufferedSource = io.Source.fromFile( path, enc = Codec.ISO8859.name)

【讨论】:

    猜你喜欢
    • 2018-12-25
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多