读入指定列
【问题】
Suppose I would like to read input text file into lists.
File contents:
/* ignore comments */
/*TaskID <space> Duration <space> Dependency <comma> <Dependency>...*/
A 1
B 2 A(-1)
C 3 B,A
D 4 D
How do I avoid reading the comments and store each line in a proper order?
Ex:
task = {A,B,C,D}
duration = {1,2,3,4}
dependency = {, A(-1), BA, D}
【回答】
首先需要去掉开头的注释和空行,然后将剩下的数据读成二维表,再将每列拼成一行。JAVA做起来比较复杂,建议用SPL脚本嵌入JAVA来实现(参考Java 如何调用 SPL 脚本):
A |
|
1 |
=file("source.csv").read@n() |
2 |
=A1.to(4,) |
3 |
=A2.regex("(.*) (.*) (.*)";a,b,c) |
4 |
=A3.fno().(A3.field(~).concat@c()) |
A1:读取source.csv文件内容。
A2:取第4行之后的内容。
A3:对A2正则匹配,拆成二维表。
A4:将每列拼成一行。