export()

Read(880) Label: export,

Here’s how to use export() function.

A .export()

Description:

Convert a sequence into a string.

Syntax:  

A.export(x:F,…;s)

Note:

The function separates each selected fields x of records in table sequence/record sequence/sequence A with the user-defined separator and returns the result as a string. The name of the resulting field in the string is F. If x isn’t specified, then export all fields. Return a one-field string without a field name if A is a simple sequence; use separator when A’s members are sequences. A serial byte value is stored as an integer.

 If parameters x:F are absent and if A is a sequence of record, then these records should be of same structure.

   The function returns JSON strings when records or members of A is record, sequence or table sequence; and returns a hexadecimal string when records or members of A are serial byte keys.

Parameter:  

A

A table sequence/record sequence/sequence to be exported

x

Fields to be exported. If omitted, then export all fields of A

F

Name of the resulting field in the string. If omitted, then use the original field name

s

User-defined field separator. The default is tab

Option:  

@t

The column names will be written to the string as the first record

@c

Use comma as the separator when the parameter s is absent, but the user-defined separator s should take priority when there are both s and @c option

@w

Use Windows-style \r\n line break; by default, the line break is specified by the operating system.

@q

Enclose the exported text field values and headers with quotation marks

@o

Perform escaping according to the Excel rule, which treats two double quotation marks as one and does not escape the other characters Need to work with @q

Return value:

String.

Example:

 

A

 

1

=demo.query("select EID,NAME from EMPLOYEE")

 

2

=A1.export()

Parameters x, F, and s are omitted

3

=A1.export(;"|")

Specify the separator as "|"

4

=A1.export@t(EID:id,NAME:name;",")

Specify to-be-exported fields and the separator; make column names the first record and write it at the beginning of the exported string

5

[1,23,34,45]

 

A sequence

6

=A5.export()

7

=A1.export@c()

 With @c option, use default separator comma when parameter s is absent

8

=A1.export@w()

 

9

=["12\r34","aa\nbb"]

 

 

10

=A9.export()

 

11

=A9.export@q()

 With @q option, the exported content contains quotation marks

12

<xml><row><DEPTID>1</DEPTID><DEPTNAME>sale</DEPTNAME><FATHER>12</FATHER></row><row><DEPTID>10</DEPTID><DEPTNAME>create</DEPTNAME><FATHER>12</FATHER></row></xml>

 

13

=xml(A12)

Return a sequence of table sequences:

Expand members of the sequence as follows:

Then expand sub-members of the sequecne as follows:

14

=A13.export()

Return JSON strings

15

=file("D://t4.txt").import@coq()

Below is content of t4.txt:

A15’s result:

16

=A15.export@coq()

With @o option, two double quotation marks are treated as one

Note:

Format of the string: Separate the records by space, and the fields by the user-defined separator. The default separator is tab.

Related function:

f.import()

f.export()

f .export( A , x : F ,…; s )

Description:

Write a sequence/table sequence/record sequence into a file.

Syntax:

f.export( A, x:F,…;s)

Note:

 The function writes sequence/table sequence/record sequence A into file f in the form of text; it will auto-create an f, which is in the text format by default, if the file doesn’t exist (but the path can’t be auto-created).

If A is a table sequence or a record sequence, write all fields into the file if parameter x is absent. If the values of a to-be-exported field are referenced records, just write their primary key values into f. The unexportable fields in A can’t be written into f.

A must be sequence consisting of records of same data structure when it is a sequence. A one-field text file without field name will be written to file f when parameter x is absent.

By default, it writes data as JSON strings when members of A is sequence/record/table sequence. If A contains a serial byte field, the serial byte values will be written as hexadecimal strings. Use the original data type with the binary export.

Parameter:

f

A file

A

The table sequence/record sequence/sequence to be exported; use separators to export when members are sequences

x

The field to be exported. If omitted, all the fields of A that can be textualized will be exported. The sign # is used to represent a field with a ordinal number

F

Resulting field name. If omitted, then use the original field name

s

The user-defined separator used in the text file, and the default separator is tab

Option:

@t

Export field description, or the headers, as the first row of the file

@a

Append. The appended records and the original records should be of the same structure, otherwise error will be reported. Ignore @t option if the there is data in the original file; overwrite the original file by default

@b

Convert into binary file to speed up the processing and ignore @t options; won’t segment A if it is small enough, otherwise it will be segmented when being converted into the binary format; A is ordered by s when the latter is present and create a new segment whenever s is changed; s should be different for the newly-added data at append

@c

Use comma as the separator when parameter s is absent, but the user-defined separator s should take priority when the parameter is present

@w

Use Windows-style \r\n line break; by default, the line break is specified by the operating system; export a sequence of sequences as structured data where the first row is field names when @b option is also present

@q

Enclose the exported text field values and headers with quotation marks

@o

Perform escaping according to the Excel rule, which identifies two double quotation marks as one and does not escape the other characters

Example:

Write a record sequence into a txt file.

 

A

 

 

1

=demo.query("select * from DEPARTMENT")

 

 

2

=file("D:\\Department.txt").export(A1)

 

3

=file("D:\\Department.txt").export@t(A1)

Use @t to export field headers in the first row as the title

 

4

=file("D:\\Department2.txt").export(A1; "|")

 

5

=file("D:\\Department2.txt").export(A1;)

 

6

=file("D:\\Department2.txt").export@a(A1)

Still the above example, append the contents from A1 to the contents of the file

 

7

=file("D:\\Department3.txt").export@b(A1)

Export the file content in binary format

 

8

=file("D:\\Department4.txt").export@t(A1,DEPT:Dept1;"|")

If not omitting x, then only export the specified field

 

9

=file("D:\\dept1.txt").export@ta(A1)

 

Since dept1.txt does not exist, create it after the execution begins and then import the first row as the title

 

10

[a,s,d,f]

 

 

11

=file("D:\\myfile.txt").export(A10)

myfile.txt is a one-field file without a field name

 

12

=file("D:\\Department6.txt").export@c(A1)

 

13

=demo.query("select * from EMPLOYEE order by GENDER")

Sort by GENDER

 

14

=file("D:\\EMPLOYEE.btx").export@b(A13,EID,NAME,SURNAME,GENDER,SALARY;GENDER)

Export the result returned by A13, which is by default ordered by GENDER to EMPLOYEE.btx

 

15

=file("D:\\EMPLOYEE.txt").export@w(A1)

Use Windows-style \r\n line break

16

=demo.query("select * from DEPARTMENT").keys(MANAGER)

 

17

=A13.switch(DEPT,A16:DEPT)

18

=file("D:\\EMPLOYEE1.txt").export@t(A16)

Values of DEPT field in A16 are referenced records; in this case the primary key of the table sequence need to be exported

19

=demo.query("select * from DEPARTMENT")

20

=file("D:\\Department7.txt").export(A19,#1)

#1 represents the first column, so only that column of data is exported

21

=file("D:\\Dep1.txt").export@q(A19,#1)

22

f1,f2,f3

2,"dd""ff",3

 

23

=A22.import@coq()

24

=file("D://t5.txt").export@coq(A23)

With @o option, two double quotation marks in each string are treated as one; the exported content in t5.txt is as follows:

Note:

Format of text file: Separate records by carriage return, and fields by the user-defined separator. The default separator is tab.

Related functions:

f.import()

f .export( cs , x : F ,…; s )

Description:

Retrieve data from a cursor and write it to a text file.

Syntax:

f.export(cs,x:F,…;s)

Note:

The function writes data of cursor cs to the file object f as text. If the file does not exist, create it automatically (Note that the directory path cannot be created automatically).

Option:

@t

Export field description, or the headers, as the first row of the file.

@a

Append. If omitted, then overwrite the original file. The option and @t are mutual exclusive. f and cs must be of the same structure, otherwise error will be reported.

@b

Convert into binary file to speed up the processing. It ignores @t and does not support the parameter s.

@c

Use comma as the separator when the parameter s is absent, but the user-defined separator s should take priority when there are both s and @c option.

@z

Force the execution of @b and write the sequence to the binary file f in segment by segment. The sequence won’t be segmented without this option. With this option, the parameter s is the grouping expression ruling that sequence A is ordered by s, if the parameter exists, and perform the segmentation only when s changes. The binary file is used to export a large sequenece in segments with parallel processing, which will make sure records in each segment won’t be split. 

@w

Use Windows-style \r\n line break; by default the line break is specified by the operating system.

@q

Enclose the exported text field values and headers with quotation marks

@o

Use quotation marks as the escape character; need to work with @q. Without it the escape character is the Java-style slash (\)

Parameter:

f

File object.

cs

Cursor whose data to be exported.

x

Fields to be exported. If omitted, then export all fields in the record sequence A that can be textualized. The sign # is used to represent a field with a ordinal number.

F

Resulting field name. If omitted, then use the original field name.

s

User-defined separator; the default is tab.

Example:

Write the cursor data to the text file.

 

A

 

 

1

=demo.cursor("select * from DEPARTMENT")

 

 

2

=file("D:\\Department1.txt").export(A1)

Tab separator.

 

3

=demo.cursor("select * from DEPARTMENT")

 

 

4

=file("D:\\Department2.txt").export(A3;"/")

Specify the separator “/”.

 

5

=demo.cursor("select * from DEPARTMENT")

 

 

6

=file("D:\\Department3.txt").export@t(A5)

Write field names in the first row as the titles

 

 

7

=demo.cursor("select * from DEPARTMENT")

 

 

8

=file("D:\\Department4.txt").export@t(A7,DEPT:Dept1,DEPT:Dept2;"|")

If x is not omitted, then export the specified fields.

 

9

=demo.cursor("select * from DEPARTMENT")

 

 

10

=file("D:\\Department5.txt").export@t(A9,DEPT,MANAGER;"/")

If F is omitted, then use the original field names.

 

11

=demo.cursor("select * from DEPARTMENT")

 

 

12

=file("D:\\Department5.txt").export@a(A11 DEPT,MANAGER;"/")

Still the above examlple, append the contents of A12 to the contents of Department5.txt.

 

13

=demo.cursor("select * from DEPARTMENT")

 

 

14

=file("D:\\Department6.txt").export@b(A13)

Export data as the binary file to get a higher speed.

 

15

=demo.cursor("select * from DEPARTMENT")

 

 

16

=file("D:\\Departmen7.txt").export@c(A15)

Use comma as the separator

 

17

=demo.cursor("select * from EMPLOYEE order by GENDER")

Sort by GENDER.

 

18

=file("D:\\EMPLOYEE1.btx").export@z(A17,EID,NAME,SURNAME,GENDER,SALARY;GENDER)

Export a segmented binary homo-name files grouped by GENDER to EMPLOYEE.btx

 

19

=demo.cursor("select * from EMPLOYEE order by GENDER")

 

20

=file("D:\\EMPLOYEE2.txt").export@w(A19)

Use Windows-style \r\n line break.

21

=demo.cursor("select * from DEPARTMENT")

 

22

=file("D:\\Department8.txt").export(A21,#1)

#1 represents the first column, so only that column in the cursor is exported

23

=file("D:\\Dep1.txt").export@q(A21)

Both exported field values and headers in the text file are quoted

24

=["12\r34","aa\nbb"].cursor()

Above is the content of the cursor

25

=file("D:\\Dep2.txt").export@q(A24)

26

=file("D:\\Dep3.txt").export@qo(A24)

Use double quotations as the escape character

Related function: 

f.export()