【问题】
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 脚本)。
本文介绍如何使用Python或Java将包含个人信息的订单表单数据转换成CSV格式。示例中提供了从姓名字段提取数据的Python脚本流程,并提及了将此脚本与Java集成的可能性。
686

被折叠的 条评论
为什么被折叠?



