多行分一组

【问题】

I need help getting data from an order form and then putting it into a CSV

Example order form:

**

First Name:John

Last Name:Smith

Middle Name:Lucy

**

I need to make a script that lets me change the order form to a csv that has the order details in order. I'm thinking about using Python, but java is my strong point.

Example csv:

John,Smith,Lucy

【回答】

源文件要处理的数据行用**来分隔,最终只需要把相邻有数据的行拼成1行。用Python做也就是循环读入处理,会有些繁琐,而且基本上不可能集成进Java。用SPL实现要简单许多:

A
1=file("d:\\OrderForm.txt").read@n()
2=A1.group@o(~=="**")
3=A2.(~.regex(".*:(.*)").(#1))
4=A3.select(~!=[])
5=A4.(~.concat@c())
6=file("d:\\result.csv").write(A5)

A1:读取OrderForm.txt中的内容并返回成字符串,每行作为一个成员。

A2:按照字符串是否等于**,进行相邻对比分组。

A3:用正则表达式匹配序列中的字符串成员。

A4:筛选成员为非空的序列。

A5:用逗号连接序列成员返回成字符串。

A6:将A5的结果写入result.csv文件。

上述代码很容易和JAVA集成(可参考Java 如何调用 SPL 脚本)。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值