拆分文本文件并仅返回 scala 中的末尾
Splitting text files and returning only the end in scala
我想从名为“c.txt”的 scala 中读取一个文本文件,其中包含
CatManC_NAA
Foranz_CSW
Foresan_SQW
和return只是txt文件的第二部分作为字符串。例如:NAA、CSW 在输入“CatManC”或“Foranz”作为参数时忽略大写。
这是我目前的代码。
object Test {
def getCode(fileName: String, nameInput:String): String={
code:String=""
for (line <- Source.fromFile(filename).getLines) {
line.split("_")
if (nameInput==line){
line.takeRight(3)
code+=line
}
else{
return ""
}
}
code
}
我是编码方面的业余爱好者,也是这门语言的新手。任何帮助将不胜感激。
我建议这样写:
def getCode(fileName: String, nameInput: String): String = {
import scala.util.control.Breaks
var code = ""
val source = Source.fromFile(fileName)
val loopBreak = new Breaks
loopBreak.breakable {
for (line <- source.getLines()) {
if (line.toLowerCase.contains(nameInput.toLowerCase)) {
code = line.split("_")(1)
loopBreak.break()
}
}
}
source.close()
code
}
这里Breaks需要在找到我们要查找的内容时停止读取文件。方法 toLowerCase 需要忽略文件和输入两边的大写。
这将是做你想做的事情的惯用、安全和有效的方式。
import scala.io.Source
import scala.util.{Try, Using}
def getCode(fileName: String, nameInput: String): Try[Option[String]] = {
val key = nameInput.toLowerCase
Using(Source.fromFile(fileName)) { source =>
source.getLines().map(_.split('_').map(_.toLowerCase).toList).collectFirst {
case `key` :: value :: Nil => value
}
}
}
Try
用于表示处理文件可能由于多种原因而失败;例如该文件不存在。
Option
用于表示 nameInput
键可能不存在,因此可能不会输出。
理想情况下,您可以处理这两种错误,可能是通过将所有内容都变成 Either[Error, String]
这样您就不必处理嵌套;像这样:
import scala.io.Source
import scala.util.{Failure, Success, Try, Using}
sealed abstract class GetCodeError(cause: Option[Throwable]) extends Throwable(cause.orNull)
object GetCodeError {
final case class FileError(cause) extend GetCodeError(cause = Some(cause))
final case object KeyNotFoundError extends GetCodeError(cause = None)
}
def getCode(fileName: String, nameInput: String): Either[GetCodeError, String] = {
val key = nameInput.toLowerCase
val result = Using(Source.fromFile(fileName)) { source =>
source.getLines().map(_.split('_').map(_.toLowerCase).toList).collectFirst {
case `key` :: value :: Nil => value
}
}
result match {
case Sucess(opt) =>
opt.toRight(left = GetCodeError.KeyNotFoundError)
case Failure(ex) =>
Left(GetCodeError.FileError(cause = ex))
}
}
或者你可能只是忘记那些错误,让程序在出现问题时崩溃;像这样:
import scala.io.Source
import scala.util.Using
def getCode(fileName: String, nameInput: String): String = {
val key = nameInput.toLowerCase
Using.resource(Source.fromFile(fileName)) { source =>
source.getLines().map(_.split('_').map(_.toLowerCase).toList).collectFirst {
case `key` :: value :: Nil => value
}.get
}
}
但是请不要在真实项目中这样做。虽然,这对于快速脚本来说可能没问题。
如果您对代码有任何疑问,请随时向他们提问!
我想从名为“c.txt”的 scala 中读取一个文本文件,其中包含
CatManC_NAA
Foranz_CSW
Foresan_SQW
和return只是txt文件的第二部分作为字符串。例如:NAA、CSW 在输入“CatManC”或“Foranz”作为参数时忽略大写。 这是我目前的代码。
object Test {
def getCode(fileName: String, nameInput:String): String={
code:String=""
for (line <- Source.fromFile(filename).getLines) {
line.split("_")
if (nameInput==line){
line.takeRight(3)
code+=line
}
else{
return ""
}
}
code
}
我是编码方面的业余爱好者,也是这门语言的新手。任何帮助将不胜感激。
我建议这样写:
def getCode(fileName: String, nameInput: String): String = {
import scala.util.control.Breaks
var code = ""
val source = Source.fromFile(fileName)
val loopBreak = new Breaks
loopBreak.breakable {
for (line <- source.getLines()) {
if (line.toLowerCase.contains(nameInput.toLowerCase)) {
code = line.split("_")(1)
loopBreak.break()
}
}
}
source.close()
code
}
这里Breaks需要在找到我们要查找的内容时停止读取文件。方法 toLowerCase 需要忽略文件和输入两边的大写。
这将是做你想做的事情的惯用、安全和有效的方式。
import scala.io.Source
import scala.util.{Try, Using}
def getCode(fileName: String, nameInput: String): Try[Option[String]] = {
val key = nameInput.toLowerCase
Using(Source.fromFile(fileName)) { source =>
source.getLines().map(_.split('_').map(_.toLowerCase).toList).collectFirst {
case `key` :: value :: Nil => value
}
}
}
Try
用于表示处理文件可能由于多种原因而失败;例如该文件不存在。
Option
用于表示 nameInput
键可能不存在,因此可能不会输出。
理想情况下,您可以处理这两种错误,可能是通过将所有内容都变成 Either[Error, String]
这样您就不必处理嵌套;像这样:
import scala.io.Source
import scala.util.{Failure, Success, Try, Using}
sealed abstract class GetCodeError(cause: Option[Throwable]) extends Throwable(cause.orNull)
object GetCodeError {
final case class FileError(cause) extend GetCodeError(cause = Some(cause))
final case object KeyNotFoundError extends GetCodeError(cause = None)
}
def getCode(fileName: String, nameInput: String): Either[GetCodeError, String] = {
val key = nameInput.toLowerCase
val result = Using(Source.fromFile(fileName)) { source =>
source.getLines().map(_.split('_').map(_.toLowerCase).toList).collectFirst {
case `key` :: value :: Nil => value
}
}
result match {
case Sucess(opt) =>
opt.toRight(left = GetCodeError.KeyNotFoundError)
case Failure(ex) =>
Left(GetCodeError.FileError(cause = ex))
}
}
或者你可能只是忘记那些错误,让程序在出现问题时崩溃;像这样:
import scala.io.Source
import scala.util.Using
def getCode(fileName: String, nameInput: String): String = {
val key = nameInput.toLowerCase
Using.resource(Source.fromFile(fileName)) { source =>
source.getLines().map(_.split('_').map(_.toLowerCase).toList).collectFirst {
case `key` :: value :: Nil => value
}.get
}
}
但是请不要在真实项目中这样做。虽然,这对于快速脚本来说可能没问题。
如果您对代码有任何疑问,请随时向他们提问!