Expanding Records & Inversed Grouping
【Question】
I have a tab-delimited file with three columns (Name, Nr1, Nr2), as shown below:
ABC 201 215
DEF 301 320
GHI 350 375
I would like to transfer the last file into the following format:
ABC 201 201 #taking the value from the first value from the second column and continue line by line till the second value in the third line as the following
ABC 202 202
ABC 203 203
......and so on till the third column value
ABC 215 215
DEF 301 301 ....and so on till the third column value
DEF 320 320
GHI 350 350
GHI 351 351
GHI 351 351
....
GHI 375 375
Is that possible in Python? I would really appreciate your help.
【Answer】
For each record in the source file, loop it over from the 2nd field value and repeat it to make a new record until the 3rd field value is reached; then union all these records. The algorithm involves multilevel loop. It’s effortless to do it with a two-liner in SPL (Structured Process Language):
A |
|
1 |
=file("d:/file.txt").import() |
2 |
=A1.news(to(#2,#3);A1.#1,~,~) |
A1: Import file.txt.
A2: For every record in A1, repeat a value from the 2nd field, plus one each time until the value of the 3rd field is reached to make a new record. Then union all these records.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
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