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

Read(1520) Label: sequence, write to a file,

Description:

Write data of a sequence/cursor to a file.

Syntax:

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

Note:

The function computes expression x on sequence/cursor A/cs and writes the data to file f in the form of text.

 

Write all fields to the file when parameter x is absent; if values of a to-be-exported field are referenced records, just write their primary key values to f. F is the field name of x in the result set.

 

When file f does not exist, automatically create the file (cannot automatically create the path directory). The created file is of text format by default.

 

When A is a sequence and parameter x is absent, write data to file f in the form of single-column text without field name.

 

By default, write data as JSON strings when members of each member of A are sequences/records/table sequences.

 

If A contains a serial byte field, write the serial byte values as hexadecimal strings; use the original data type with the binary export. The unexportable fields (such as images) in A can’t be written into file f.

 

Format of the txt file: separate records by carriage return; separate fields by the custom delimiter, which is tab by default.

Parameter:

f

A file

A/cs

The sequence/cursor 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 sequence number

F

Result field name; if omitted, then use x

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 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;

With the option, parameter s represents a field or field expression of sequence A; when parameter s is present, A is regarded as ordered by s and create a segment only when 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

@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 table sequence to txt file:

 

A

 

1

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

 

2

=file("Dep.txt").export(A1;"|")

Write all fields in table sequence A1 to file, use | as the separator, and generate a file as follows:

Use @t option to write the first row as the title:

 

A

 

1

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

 

2

=file("Dep.txt").export@t(A1)

 

Generate a file as follows:

Use @a option to append data to file:

 

A

 

1

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

 

2

=file("Dep.txt").export@a(A1)

Append A1’s content to its end.

Export specified fields:

 

A

 

1

=demo.cursor("select EID,NAME,BIRTHDAY,SALARY from EMPLOYEE")

 

2

=file("emp.txt").export@t(A1,NAME,age(BIRTHDAY):Age)

Export cursor A1’s NAME field and BIRTHDAY field; set Age as the result field name for computing BIRTHDAY field expression.

3

=file("emp2.txt").export@t(A1,#1,#2)

Use #1 and #2 to represent the first and the second fields in A1.

Use @b option to write data to bin file:

 

A

 

1

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

 

2

=file("Dep.btx").export@b(A1)

The bin file to which data is written has column titles by default.

Use @b option to export as a segmented bin file:

 

A

 

1

=10000.new(~:id,rand(10):xb).sort(xb)

Generate a table sequence ordered by xb field.

2

=file("stest.btx").export@b(A1;xb)

Write data as a bin file segmented by xb field, during which a new segment is created when xb field value is changed.

Write data of a sequence and parameter x is absent:

 

A

 

1

[a,s,d,f]

 

2

=file("myfile.txt").export(A1)

Generate a single-column file without the field name.

When values of the to-be-exported field in the table sequence are referenced records:

 

A

 

1

=demo.query("select EID,NAME,DEPT from EMPLOYEE order by GENDER")

 

2

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

 

3

=A1.switch(DEPT,A2:DEPT)

DEPT values are referenced records.

4

=file("empswtich.txt").export@t(A3)

The exported file is as follows:

Write data as JSON string:

 

A

 

1

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

 

2

=A1.group(DEPT).new(~:DEPT)

Return a sequence whose members are records:

3

=file("empj.txt").export(A2)

Export the file as JSON strings:

Use @q option to generate a text file where both field values and column headers are quoted:

 

A

 

1

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

 

2

=file("Dep.txt").export@qt(A1,#1)

Use @o option to treat two continuous quotation marks as one:

 

A

 

1

f1,f2,f3

2,"dd""ff",3

 

2

=A1.import@coq()

 

=file("o.txt").export@coq(A2)

As @o option is present, treat the two quotation marks in the string as one and the exported t5.txt is as follows:

Related function:

f.import()