9.16 Parse string as table

 

Parse a string as a table sequence.
On system clipboard there is the copy of GDP and population data of major cities in China, and we are trying to output it as a csv file. Below is part of the data:

ID	City	GDP	Population
1	Shanghai	32679	2418
2	Beijing	30320	2171
3	Shenzhen	24691	1253
4	Guangzhou	23000	1450
5	Chongqing	20363	3372
6	Tianjin	18809	1557
7	Suzhou	18597	1068
8	Chengdu	15342	1605
…

In SPL, S.import(;s) function takes content read from string S as records and return them as a table sequence. Parameter s specifies the delimiter; the default is tab. @t option means reading the first row as title.

SPL script:

A
1 =clipboard()
2 =A1.import@t()
3 >file(“GDP.csv”).export@ct(A2)

A1 Return clipboard content in the form of a string.
A2 Import the string as a table sequence, during which the delimiter is the default tab (\t). @t means importing the first row as title.
A3 Export A2’s table sequence to GDP.csv.

Execution result:

ID,City,GDP,Population
1,Shanghai,32679,2418
2,Beijing,30320,2171
3,Shenzhen,24691,1253
4,Guangzhou,23000,1450
5,Chongqing,20363,3372
…