Data Loaders
Data loaders provide a convenient way to process data from various data files in your streams. You can access the values of each data set as if it were an object, containing the header names as attributes.
Currently, PyStreamAPI supports reading data from CSV, JSON, XML and YAML files.
To use the loaders, you can import them with this line:
CSV loader
In order to load the data from a CSV file, you can use the csv
loader.
You just need the file's path, and you can optionally specify the delimiter and the encoding. By default, the encoding is set to UTF-8.
By default, all values get converted to int
, float
, bool
or otherwise str
. The type casting can be disabled to speed up the reading time by setting the cast_types
parameter to False
.
The examples below use this CSV file:
If you want to disable type conversion, you can use the loader like this:
JSON loader
In order to load the data from a JSON file, you can use the json
loader.
You can read data either from a JSON file or a string containing JSON. If you read from a string you have to set the read_from_src
parameter to True
.
By default, all values get converted to int
, float
, bool
or otherwise str
.
The example below uses this JSON file:
If you want to pass the JSON directly as a string, you can do it like that:
XML loader
In order to load the data from an XML file, you can use the xml
loader.
The loader isn't included in the core version of pystreamapi. You can install it using the following command:
🎉 Now you can use the loader as described below!
You just need the file's path, and you can optionally specify the encoding. By default, the encoding is set to UTF-8.
You can read data either from an XML file or a string containing XML. If you read from a string, you have to set the read_from_src
parameter to True
.
By default, all values get converted to int
, float
, bool
or otherwise str
. The type casting can be disabled to speed up the reading time by setting the cast_types
parameter to False
.
The XML loader directly retrieves the children nodes from the XML's root. By setting the retrieve_children
parameter to False
you disable this feature and your stream will only consist of one object containing the whole XML tree.
The examples below use this XML file:
Here you can see a few examples illustrating how to access different nodes.
If you disable child retrieving, you have to map the object's children manually:
YAML loader
In order to load the data from a YAML file, you can use the yaml
loader.
You can read data either from a YAML file or a string containing YAML. If you read from a string you have to set the read_from_src
parameter to True
.
By default, all values get converted to int
, float
, bool
or otherwise str
.
The example below uses this JSON file:
If you want to pass the YAML directly as a string, you can do it like that:
Last updated