CSV等类型文件与Map互相转换(Scala)
前提
数据读入转换为List[Map[String,String]]
/**
* 从 带表头的CSV文件转换为List[Map[String,String]]
* @param fileName
* @separator 分隔符指定输入文件分隔符
* @return
*/
def fromFileToMap(fileName:String,separator:String) ={
val reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"utf-8") )
var flag = true;
// 解析第一行 第一行为表头 map的key
val head=reader.readLine()
var result = List[Map[String,String]]()
while (flag){
val line = reader.readLine()
if(line==null){
flag=false
}else{
val heads = head.split(s"""${separator}""")
val values = line.split(s"""${separator}""")
val tuples = heads.zip(values).toMap // 相当于key和value 进行拉链
result=tuples::result
}
}
reader.close()
result
}
带表头字段写出
/**
*
* @param datalist 数据集
* @param outPutFilePath 输出文件路径
* @param separator 分隔符
*/
def fromMapToFile(datalist:List[Map[String,String]],outPutFilePath:String,separator:String)={
// 获取Map的key
if(datalist.nonEmpty){
val writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outPutFilePath,true),"utf-8"))
val head = datalist.head.keySet.toArray
val headLine = head.mkString(s"""${separator}""")
writer.write(headLine)
writer.newLine()
datalist.foreach(line=>{
var line_value = List[String]()
head.foreach(key=>
{
line_value= line.getOrElse(key,"")::line_value
}
)
writer.write(line_value.reverse.mkString(s"""${separator}"""))
writer.newLine()
})
writer.close()
}
}
测试
name,age,sex,tel
zhangsan,20,女,134567
lisi,21,男,134567
wangwu,22,男,134567
name age sex tel
wangwu 22 男 134567
lisi 21 男 134567
zhangsan 20 女 134567
def main(args: Array[String]): Unit = {
val dataList = fromFileToMap("E:\\demo\\userinfo.csv",",") //输入数据CSV
fromMapToFile(dataList,"E:\\demo\\userinfo_out.csv","\t")//输出文件制定为TSV
}