table load ...

Prev Next

Function Names

table load, table load unchanged

Description

This function loads a table from a file. Following file formats are supported:

CSV Comma separated files Files separated with a specified symbol, typically a comma, semicolon or tab
TEXT Text file Loads text files into a table consisting of 1 column. Every column contains one line of text.
HTML HTML file Loads a specifically selected table from the HTML file
FULL HTML HTML file Loads a complete HTML file into a sequential table
XML XML (extended markup) file Loads a complete XML file into a sequential table
JSON JavaScript Object Notation format Loads a complete JSON file into a sequential table
JSON TABLE JavaScript Object Notation format Loads a specifically selected JSON table (which is a 2-dimensional array) into a table

For CSV file formats, the function table load will automatically convert numbers with decimal commas (e.g. 123,456) to numbers with decimal points (123.456) so they can be processed further and conveniently. This feature is particularly useful with processing data originating from databases or Excel with settings specific to Germany and some other countries which use decimal commas. If this automatic conversion is not desired because the table data entries with numbers separated with a comma but seen as actual numbers but possibly a set of two digits, then use the function table load unchanged instead. In this case, value like 123,456 remain unchanged. Various database systems, including SAP, Oracle, Salesforce which support exports as "Excel" files are in fact HTML or file formats.

A different and dedicated function called table load excel file() is available to load contemporary Excel files with the endings .xlsx, and .xlsm. Prerequisite: File is not encrypted.

Recognizing character formats: B4P tries to automatically identify whether the file is ANSI (ASCII), Win 1252, iso8859-1, and UNICOD: UTF-8, UTF-16 or UTF-16 Big Endian. The identification is done by preambles in HTML, byte order marks, usage of non-ANSI characters and typical patterns in UTF-16 formats.

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled

Parameter count

2-4

Parameters

No.TypeDescription
1
input
string Name of table

If the table is existing, then it will be initialized first.

2
input
string File name

Specify the file name. Add the path in front if the file is not located in the current working directory.

Opt. 3
input
string File format descriptor

Value Description
, CSV with comma as separator. (Put it into quotation marks ',' when specifying it in the function parameter)
; CSV with semicolon as separator. (Put it into quotation marks ';' when specifying it in the function parameter)
? CSV with automatic detection of either comma, semicolon or tab. The first occurence of these symbols will be used as separators. (Put it into quotation marks '?' when specifying it in the function parameter)
tab TSV using tab as separator (Do not use quotation marks here because tab is a reserved B4P keyword representing a single tab character)
CSV Same functionaliy as '?' with automatic dection of separator symbol
1 character CSV with any other single character, e.g. "/" as separator
TEXT Text file. The resulting table contains only one column where the column in every row contains the whole line of text.
HTML Loads HTML file. See section on loading HTML files.
FULL HTML Loads HTML file. See section on loading HTML files. (Same functionality for time being)
XML Loads XML file. See section on loading XML files.
JSON Loads entire JSON file. See section on loading JSON files.
JSON TABLE Loads a specific table (coded as an array) from the JSON file. See section on loading JSON files.

Default value: CSV or ?
Opt. 4
input
set XML attributes to keep

Applicable to file format descriptor XML only:
Applicable for file format descriptor XML only: Specify the attributes in XML tags to keep. The remaining ones will not be loaded. This feature is particularly useful for very large XML files.

Default value: (Keep all attributes)
Alt. Opt. 4
input
string Text pattern

Applicable to file format descriptors HTML, MJTML, JSON TABLE only:
Specify a text sequence, e.g. JSON elements, HTML elements, etc. which will be checked against the JSON and HTML text. This feature is insofar useful when the HTML and JSON files contain multiple tables and a table other than the 1st table should be loaded. In this case, find a unique text, e.g. a section name or distinct contents which appears before the table to load, but after the previous tables in the same file.

Attention: Wildcards are supported for JSON, but not for HTML and FULL HTML. Example: ' "name" : "C*" ' looks for "name" followed by ":" and followed by "C..." (beginning with "C").

Advice: Put the whole string in single quotation marks beacuse JSON and HTML syntax rules require text contents to be specified in double quotation marks, e.g. 'id="NASDAQ_Table"'.

Advice: Some HTML links contain a pound symbol, e.g. "en.wikipedia.org/wiki/List_of_Roman_emperors#The_Principate". To access the HTML table below this identifier "The_Principate" which is, typical to Wikipedia, the section header "The Principate", download the entire HTML file and specify 'id="The_Principate"' in this parameter to find next table located below.

Default value: (look for 1st table)

Return value

TypeDescription
boolean Table has contents

true if contents have been loaded, false if nothing has been loaded. In this case, the table is empty.

Exceptions

File not found
File not accessible
File locked
Syntax errors in table formats, e.g in HTML, JSON, XML
File locked. Close the file in the other application and retry

Examples

       runtime settings[verbose]= quiet; // Supress blabla

       echo("Load the Cities file:");
       table load( cities, "Examples\Cities.csv" );
       echo("Cities: ", join( [cities:City,..], ", " ) );

Output

Load the Cities file:
Cities: New York City, Washington, Philadelphia, Boston, San Francisco, Montréal, Copenhagen, Venice, Los Angeles, Vienna, Bangkok, Zürich, Paris, Davos
Try it yourself: Open LIB_Function_table_load.b4p in B4P_Examples.zip. Decompress before use.

See also

table save
table load excel file
loading XML files
loading JSON files