关闭!!我如何从Scala中的字符串检测类型? [英] CLOSED!! How i can detect the type from a string in Scala?
问题描述
我正在尝试解析csv文件,我需要从其字符串值开始确定每个字段的类型。
I'm trying to parse the csv files and I need to determine the type of each field starting from its string value.
例如:
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的解决方案。
我希望将来有人可以提供服务。
This is the solution I've found to recognize the fields String, Date, Int, Double and Boolean. I hope that someone can serve in the future.
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"
}
}
推荐答案
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
相匹配。
But in all honesty I wouldn't use this approach, this is so error prone and there are so many things that could go wrong that I don't even want to think about it, the simplest example is what if a string contains a /
, this small piece of code would match that as a Date
.
I不知道您的用例,但以我的经验,创建试图猜测不安全数据类型的东西总是一个坏主意,如果您可以控制它,则可以引入一些标识符,例如 1/1/06 0:00%d%
其中%d%
将指示日期,依此类推,然后将其从字符串中删除,即使那样,您也永远不会百分百确定这不会失败。
I don't know your use-case but in my experience it's always a bad idea to create something that tries to guess types form unsecure data, if you have control over it you could introduce some identifier, for example "1/1/06 0:00 %d%"
where %d%
would indicate a date and so on and then remove it from the string, and even then you'll never be 100% sure that this won't fail.
这篇关于关闭!!我如何从Scala中的字符串检测类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!