在Scala中,如何通过一对键值查找CSV中的elemein?
例如,从以下文件中:
Name,Surname,E-mail John,Smith,[email protected] Nancy,Smith,[email protected] Jane,Doe,[email protected] John,Doe,[email protected]
如何获取 John Doe 的电子邮件地址?
我现在使用以下代码,但现在只能指定一个关键字段:
val src = Source.fromFile(file)
val iter = src.getLines().drop(1).map(_.split(","))
var quote = ""
iter.find( _(1) == "Doe" ) foreach (a => println(a(2)))
src.close()
我尝试编写 "iter.find( _(0) == "John" && _(1) == "Doe" )" ,但这会引发一个错误,指出只需要一个参数(将条件括在额外的一对括号中没有帮助)。
For example, from a following file:
Name,Surname,E-mail John,Smith,[email protected] Nancy,Smith,[email protected] Jane,Doe,[email protected] John,Doe,[email protected]
how do I get e-mail address of John Doe?
I use the following code now, but can specify only one key field now:
val src = Source.fromFile(file)
val iter = src.getLines().drop(1).map(_.split(","))
var quote = ""
iter.find( _(1) == "Doe" ) foreach (a => println(a(2)))
src.close()
I've tried writing "iter.find( _(0) == "John" && _(1) == "Doe" )", but this raises an error saying that only one parameter is expected (enclosing the condition into extra pair of parentheses does not help).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下划线作为 lambda 参数的占位符并不像您想象的那样工作。
即第一个下划线表示第一个参数,第二个下划线表示第二个参数,以此类推。这就是您所看到的错误的原因——您使用了两个下划线,但只有一个参数。修复方法是使用显式版本:
The underscore as a placeholder for a parameter to a lambda doesn't work the way that you think.
That is, the first underscore means the first parameter and the second one means the second parameter and so on. So that's the reason for the error that you're seeing -- you're using two underscores but have only one parameter. The fix is to use the explicit version:
您可以使用正则表达式:
You can use Regex:
您还可以在
for
理解中对split
的结果进行模式匹配。请注意模式中的反引号:这意味着我们匹配
firstName
的值,而不是引入一个与任何内容匹配的新变量并隐藏val firstname
。You could also do a pattern match on the result of
split
in afor
comprehension.Note the backticks in the pattern: this means we match on the value of
firstName
instead of introducing a new variable matching anything and shadowing theval firstname
.