读取csv文件并在scala中的不同位置添加换行符

时间:2017-11-26 09:07:33

标签: scala

这是我的数据集:

UNIT_TYPE,ALARM_TYPE,ALARM_INFO,LOCATION,RAISED_TIME,ALARM_GROUP,PROBABLE_CAUSE,(\n)
    "ECP,OPTIC_TRANSCEIVER_RX_LOS,RACK[0]/SHELF[0]/SLOT[1]-ECP[0]/CPRI_PORT[10],2017-08-31,14:30:48,EQUIPMENT_ALARM,LINE_INTERFACE_FAILURE,major,3116378,When,optic,cable,open,or,optical,signal,strength,decreases.,4",(\n)

需要的模式:

<?php
include('connect.php');
$nrcrt=1;
$sql="SELECT Cod, Nume, Categorie, Tumb, Tabel, Descriere FROM produse";
$sqlcateg="SELECT categ FROM categorii";
$resultcateg=$conn->query($sqlcateg);
$result=$conn->query($sql);

if($result->num_rows>0)
{
    while($row=$result->fetch_assoc())
        {   
            echo    '<tr>
                        <td>'.$nrcrt.'</td>
                        <td>'.$row['Cod'].'
                                <script type="text/javascript">
                                function editcod'.$nrcrt.'() {
                                var x = document.getElementById("fieldscod'.$nrcrt.'");
                                if (x.style.display === "none") {   
                                    x.style.display = "block";
                                } else {
                                    x.style.display = "none";
                                }
                                };
                                </script>
                                <br> <br> 
                                <button onclick="editcod'.$nrcrt.'()">Editare</button>
                                <br>
                                <div id="fieldscod'.$nrcrt.'"hidden><input type="text" id="fieldupdatecod" value="'.$row['Cod'].'">
                                <br>
                                <input id="updatecod" type="submit" value="Update"></div>
                        </td>
                        <td>'.$row['Nume'].'
                                <script type="text/javascript">
                                function editnume'.$nrcrt.'() {
                                var x = document.getElementById("fieldsnume'.$nrcrt.'");
                                if (x.style.display === "none") {   
                                    x.style.display = "block";
                                } else {
                                    x.style.display = "none";
                                }
                                };
                                </script>
                                <br> <br>
                                <button onclick="editnume'.$nrcrt.'()">Editare</button>
                                <br>
                                <div id="fieldsnume'.$nrcrt.'" hidden><input type="text" id="fieldupdatenume" value="'.$row['Nume'].'">
                                <br>
                                <input id="updatenume" type="submit" value="Update"></div>
                        </td>

这个数据集是一个以逗号分隔的数据集,但问题是它全部都在一个字符串中,结果应该在不同的行中,在我的情况下,它们都是一行而不是不同的行,所以我可以将它与excel表相关联。

我需要在PROBABE_CUASE之后添加一个换行符(\ n),然后在4&#34;之后再添加换行符。 我实际上是在创建一个CSV文件,但无法添加 / n 来分隔我的行 可以有人帮忙

1 个答案:

答案 0 :(得分:-1)

要解决这个问题,我将标识符替换为&amp;然后给了一个新的线条角色。 #

我的代码:

package learningscala
import scala.io.Source
import scala.xml.XML
import scala.util.matching.Regex
import java.io.{File, FileOutputStream}

object hello {
  def main(args: Array[String] ) {

    val fos = new FileOutputStream(new File("/home/aditya/Downloads/file.txt"))
Console.withOut(fos) {

   println("UNIT_TYPE,ALARM_TYPE,ALARM_INFO,LOCATION,RAISED_TIME,ALARM_GROUP,PROBABLE_CAUSE \t\n")

  // bufferedSource = io.Source.fromFile("/home/aditya/Downloads/sample.csv")
    //for (line <- bufferedSource.getLines) {
       //prints the specific line 
      //println(line)

        // do whatever you want with the columns here
      val lines = Source.fromFile("/home/aditya/Downloads/sample.csv")
      //println(lines)
      val regex = new Regex("EQUIPMENT_ALARM")

       for (line <- lines.getLines())
       { 
                                           regex.findFirstIn(line) match {
                                        case Some(text) => println( line + "")
                                          //case Some(text) => println() 
                                          case None => println()

                                             }
       }

  val file = Source.fromFile("/home/aditya/Downloads/file.txt").getLines().filter(!_.isEmpty()).mkString(" &# ")
     printf(file)
    //var str = "Hello123"
val res = file.split("#")



 val fos = new FileOutputStream(new File("/home/aditya/Downloads/file1.txt"))


Console.withOut(fos) {

val res1 = println(res.deep.mkString("\n"))

  val lines1 = Source.fromFile("//home/aditya/Downloads/file1.txt").mkString
printf(lines1)
val res2 = lines1.split("\\s+")


val fos =   new FileOutputStream(new File("/home/aditya/Downloads/file2.csv"))

 Console.withOut(fos) { 

val res3 = println(res2.deep.mkString(","))
//val nameValuePairs = result.split("&")

 val lines2 = Source.fromFile("/home/aditya/Downloads/file2.csv").mkString
 //printf(lines2)
val res4 = lines2.split(",&,")



val fos =   new FileOutputStream(new File("/home/aditya/Downloads/file3.csv"))
Console.withOut(fos) {



val res5 = println(res4.deep.mkString("\n"))
val lines3 = Source.fromFile("/home/aditya/Downloads/file3.csv").mkString
val res6 = lines3.replace("\"", "")
val fos =   new FileOutputStream(new File("/home/aditya/Downloads/file3.csv"))
Console.withOut(fos) {


printf(res6)
}
}

  //val regex = new Regex("  ECP,  ")






//val res2 = lines1.split("\n").map(_.split("\\s+")(0)).mkString(", ")
//printf(res2)
//val res2 = lines1.split("\\s+")

    // val res2 = file.split("\\s+")

    // val res3 = println(res2.deep.mkString(","))
     //printf("$res3")
 //val test = "/home/aditya/Downloads/file.txt".split ("\\s+").map (_.trim).mkString ("\n")
     //val res2 = res.split("\\W")
 //val res3 = println()
    // val res2 = file.split("\\s+")
}

  }
    }
  }

}

所以,我所做的是,我创建了一个额外的标识符&amp;这帮助我成功地应用了拆分操作。

输出

UNIT_TYPE,ALARM_TYPE,ALARM_INFO,LOCATION,RAISED_TIME,ALARM_GROUP,PROBABLE_CAUSE

ECP,OPTIC_TRANSCEIVER_RX_LOS,RACK [0] / SHELF [0] / SLOT [1] -ECP [0] / CPRI_PORT [10],31-08-2017,14:30:48,EQUIPMENT_ALARM,LINE_INTERFACE_FAILURE,主要, 3116378,当,视神经,电缆,打开或,光学,信号,强度减小。,4 ,,, ECP,OPTIC_TRANSCEIVER_TX_FAULT,RACK [0] / SHELF [0] / SLOT [1] -ECP [0] / CPRI_PORT [10],31-08-2017,14:30:48,EQUIPMENT_ALARM,LINE_INTERFACE_FAILURE,主要的,3116379,当,功率,监测,结果,激光,二极管,是不正常的。,6 ,,, ECP,OPTIC_TRANSCEIVER_RX_LOS,RACK [0] / SHELF [0] / SLOT [2] -ECP [1] / CPRI_PORT [10],31-08-2017,14:30:48,EQUIPMENT_ALARM,LINE_INTERFACE_FAILURE,主要的,3116378,当,视神经,电缆,打开或,光学,信号,强度减小。,8 ,,, ECP,OPTIC_TRANSCEIVER_TX_FAULT,RACK [0] / SHELF [0] / SLOT [2] -ECP [1] / CPRI_PORT [10],31-08-2017,14:30:48,EQUIPMENT_ALARM,LINE_INTERFACE_FAILURE,主要的,3116379,当,功率,监测,结果,激光,二极管,是不正常的。,10 ,,, ECP,BOARD_DELETION,RACK [0] / SHELF [0] / SLOT [3] -ECP [2],31-08-2017,14:30:25,EQUIPMENT_ALARM,EQUIPMENT_MALFUNCTION,关键的,3116366,当时,,板,是,拆卸。,2 ,,,,,,, RRH,VSWR_FAIL,RRH [0_6_0] -RRH [0_6_0] / PATH [5],11-09-2017,01:26:23,EQUIPMENT_ALARM,TRANSCEIVER_PROBLEM,主要的,3149392,当,返回,损失,,ANT,端口是,下面的,阈值,电平。,200, SYS,COMMUNICATION_FAIL,RACK [0] / SHELF [0] / SLOT [6] -EAIU,31-08-2017,14:31:22,EQUIPMENT_ALARM,LINK_FAILURE,关键的,3199381,当在,保持活动,消息,响应,不能,是,接收到的,DU,和,EAIU之间,。,12