关闭!!我如何从Scala中的字符串中检测出类型?

时间:2014-05-14 13:52:46

标签: scala csv pattern-matching string-matching opencsv

我试图解析csv文件,我需要从字符串值开始确定每个字段的类型。

例如:

val row: Array[String] = Array("1/1/06 0:00","3108 OCCIDENTAL DR","3","3C","1115")

这就是我得到的:

row(0) --> Date
row(1) --> String
row(2) --> Int
Ecc....

我该怎么办?

------------------------------------ SOLUTION --- ---------------------------------

这是我发现识别String,Date,Int,Double和Boolean字段的解决方案。 我希望有人能够在将来服务。

  def typeDetection(x: String): String = {
    x match {
      // Matches: [12], [-22], [0] Non-Matches: [2.2], [3F]
      case int if int.matches("^-?[0-9]+$") => "Int"
      // Matches: [2,2], [-2.3], [0.2232323232332] Non-Matches: [.2], [,2], [2.2.2]
      case double if double.matches("^-?[0-9]+(,|.)[0-9]+$") => "Double"
        // Matches: [29/02/2004 20:15:27], [29/2/04 8:9:5], [31/3/2004 9:20:17] Non-Matches: [29/02/2003 20:15:15], [2/29/04 20:15:15], [31/3/4 9:20:17]
      case d1 if d1.matches("^((((31\\/(0?[13578]|1[02]))|((29|30)\\/(0?[1,3-9]|1[0-2])))\\/(1[6-9]|[2-9]\\d)?\\d{2})|(29\\/0?2\\/(((1[6-9]|[2-9]\\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1\\d|2[0-8])\\/((0?[1-9])|(1[0-2]))\\/((1[6-9]|[2-9]\\d)?\\d{2})) *(?:(?:([01]?\\d|2[0-3])(\\-|:|\\.))?([0-5]?\\d)(\\-|:|\\.))?([0-5]?\\d)")
        => "Date"
        // Matches: [01.1.02], [11-30-2001], [2/29/2000] Non-Matches: [02/29/01], [13/01/2002], [11/00/02]
      case d2 if d2.matches("^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.)31)\\1|(?:(?:0?[1,3-9]|1[0-2])(\\/|-|\\.)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:0?2(\\/|-|\\.)29\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.)(?:0?[1-9]|1\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$")
        => "Date"
        // Matches: [12/01/2002], [12/01/2002 12:32:10] Non-Matches: [32/12/2002], [12/13/2001], [12/02/06]
      case d3 if d3.matches("^(([0-2]\\d|[3][0-1])(\\/|-|\\.)([0]\\d|[1][0-2])(\\/|-|\\.)[2][0]\\d{2})$|^(([0-2]\\d|[3][0-1])(\\/|-|\\.)([0]\\d|[1][0-2])(\\/|-|\\.)[2][0]\\d{2}\\s([0-1]\\d|[2][0-3])\\:[0-5]\\d\\:[0-5]\\d)$")
        => "Date"
      case boolean if boolean.equalsIgnoreCase("true") || boolean.equalsIgnoreCase("false") => "Boolean"
      case _ => "String"
    }

}

2 个答案:

答案 0 :(得分:2)

val row: Array[String] = Array("1/1/06 0:00","3108 OCCIDENTAL DR","3","3C","1115")

val types: Array[String] = row.map(x => x match {
  case string if string.contains("/") => "Date probably"
  case string if string.matches("[0-9]+") => "Int probably"
  case _ => "String probably"
})


types.foreach( x => println(x))

输出:

Date probably
String probably
Int probably
String probably
Int probably

但是说实话,我不会使用这种方法,这很容易出错,而且有很多事情可能会出错,我甚至不想考虑它,最简单的例子就是如果一个字符串包含/,这一小段代码将与Date匹配。

我不知道你的用例,但根据我的经验,创建一些试图猜测类型形成不安全数据的东西总是一个坏主意,如果你可以控制它,你可以引入一些标识符,例如{{1其中"1/1/06 0:00 %d%"表示日期等等,然后将其从字符串中删除,即使这样,您也绝不会100%确定这不会失败。

答案 1 :(得分:0)

对于每个字符串:尝试将其解析为您想要的类型。你必须为每种类型编写一个函数。继续尝试,直到其中一个工作,订单很重要。您可以使用自己喜欢的日期/时间库。

  import java.util.Date
  def stringdetect (s : String) = {
    dateFromString(s) orElse intFromString(s) getOrElse s
  }

  def arrayDetect(row : Array[String]) = row map stringdetect

  def arrayTypes(row : Array[String]) = {
    arrayDetect(row) map { _ match {
      case x:Int => "Int"
      case x:Date => "Date"
      case x:String => "String"
      case _ => "?"
    } }
  }      

  def intFromString(s : String): Option[Int] = {
    try {
      Some(s.toInt)
    } catch {
      case _ : Throwable => None
    }
  }

  def dateFromString(s : String): Option[Date] = {
    try {
      val formatter = new java.text.SimpleDateFormat("d/M/yy h:mm")
      formatter.format(new java.util.Date)
      Some(formatter.parse(s))
    } catch {
      case _ : Throwable => None
    }
  }

来自REPL /工作表:

  val row: Array[String] = Array("1/1/06 0:00","3108 OCCIDENTAL DR","3","3C","1115")
        //> row  : Array[String] = Array(1/1/06 0:00, 3108 OCCIDENTAL DR, 3, 3C, 1115)
  arrayDetect(row)
        //> res0: Array[Any] = Array(Sun Jan 01 00:00:00 CST 2006, 3108 OCCIDENTAL DR, 3 , 3C, 1115)
  arrayTypeDisplay(row)
        //> res1: Array[String] = Array(Date, String, Int, String, Int)