根据列名读入数据并变换

【问题】

I have a requirement like:

1.     My input will be a csv file where I will be having values like below:

action, userId, firstName, email, lastName
1,2,sample,abc@gmail.com,test
2,3,justtest,def@gmail.com,test

2.     I have to read this csv file based on headers. Say for ex: if action =1 and email is null, then I have to change from action to 2 or email to null or something like this.

3.     I have no idea on how to read and parse the values based on the headers. This is my code what I tried:

String csvFile = "C:\\Test.csv";
    // create BufferedReader to read csv file
    BufferedReader br;
    String line = "";
    br = new BufferedReader(new FileReader(csvFile));
    br.readLine();
    // Read the file line by line starting from the second line
    while ((line = br.readLine()) != null) {
        // Get all tokens available in line
        String[] tokens = line.split(",");
        if (tokens.length > 0) {
            for (int i = 0; i < tokens.length; i++) {
                System.out.println("All Rows ------->" + tokens[i]);
            }
        }
    }

This is just printing all the values in new line like below:

   All Rows ------->1411184866
   All Rows ------->category
   All Rows ------->123456
   All Rows ------->Test
   All Rows ------->TestFullName
   All Rows ------->rsap@gmail.com
   All Rows ------->3423131

【回答】

       按先读入csv文件再按照列名对文件内容进行处理,使用SPL来实现要简单许多:

A
1=file("source.csv").import@ct()
2=A1.run(if(action==1 && email==null,(action=2,email="something")))

A1:读取source.csv文件内容。

A2:将记录中action=1且emai=null的数据修改为action=2,email= something。

这段代码可以方便地集成进Java,参考Java 如何调用 SPL 脚本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值