Import Certain Text Data
【Question】
I would like to read input text file into lists.
File content:
/* 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 comments and store each line in a proper order?
Desired result:
task = {A,B,C,D}
duration = {1,2,3,4}
dependency = {, A(-1), BA, D}
【Answer】
Remove comments and empty space, read the other data into a 2D table, and join up values of each column into a row. It’s complicated to do it in Java. You can handle it in SPL (Structured Process Language) and then embed the script into the Java application. (For Java invocation, see How to Call an SPL Script in Java):
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: Read in the content of source.csv.
A2: Get data from the 4th row.
A3: Match each of A2’s members with a regular expression to split the sequence into a 2D table.
A4: Concatenate values of each column into a row.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL