nncore.io
IO
- nncore.io.io.load(name_or_file, format=None, **kwargs)[source]
Load data from files.
- Parameters:
name_or_file (list | str | file object) – Paths to the files or file objects.
format (str, optional) – Format of the file. If not specified, the file format will be inferred from the file extension. Currently supported formats include
json/jsonl
,yaml/yml
,pickle/pkl
,hdf5/h5
,npy/npz
,xml
, andtxt
. Default:None
.
- Returns:
The loaded data.
- Return type:
any
- nncore.io.io.dump(obj, name_or_file, format=None, overwrite=True, **kwargs)[source]
Dump data to a file.
- Parameters:
obj (any) – The object to be dumped.
name_or_file (str | file object) – Path to the file or a file object.
format (str, optional) – Format of the file. If not specified, the file format will be inferred from the file extension. Currently supported formats include
json/jsonl
,yaml/yml
,pickle/pkl
,hdf5/h5
,npy/npz
,xml
, andtxt
. Default:None
.overwrite (bool, optional) – Whether to overwrite it if the file exists. Default:
True
.
- nncore.io.io.loads(string, format='pickle', **kwargs)[source]
Load data from strings.
- Parameters:
string (list | str | btyearray) – Strings of the data.
format (str, optional) – Format of the string. Currently supported formats include
json/jsonl
,yaml/yml
,pickle/pkl
andxml
. Default:'pickle'
.
- Returns:
The loaded data.
- Return type:
any
- nncore.io.io.dumps(obj, format='pickle', **kwargs)[source]
Dump data to a string.
- Parameters:
obj (any) – The object to be dumped.
format (str, optional) – Format of the string. Currently supported formats include
json/jsonl
,yaml/yml
,pickle/pkl
andxml
. Default:'pickle'
.
- Returns:
The dumped string.
- Return type:
str
- nncore.io.io.list_from_file(filename, encoding='utf-8', offset=0, separator=',', max_length=-1)[source]
Load a text file and parse the content as a list of tuples or str.
- Parameters:
filename (str) – Path to the file to be loaded.
encoding (str, optional) – The encoding of the file.
offset (int, optional) – The offset of line numbers. Default:
0
.separator (str | None, optional) – The separator to use for parsing tuples. If not specified, each line would be treated as a str. Default:
','
.max_length (int, optional) – The maximum number of lines to be loaded.
-1
means all the lines from the file will be loaded. Default:-1
.
- Returns:
The loaded str list.
- Return type:
list[str]
- nncore.io.io.open(file=None, mode='r', format=None, as_decorator=None, **kwargs)[source]
Open a file and return a file object. This method can be used as a function or a decorator. When used as a decorator, the function to be decorated should receive the handler using an argument named
f
. File and mode can be overrided during calls using the argumentsfile
andmode
. Argumentfile
andformat
should not beNone
at the same time.- Parameters:
file (str | None, optional) – Path to the file to be loaded.
mode (str, optional) – The loading mode to use. Default:
'r'
.format (str, optional) – Format of the file. If not specified, the file format will be inferred from the file extension. Currently supported formats include
jsonl
andhdf5/h5
. Default:None
.as_decorator (bool | None, optional) – Whether this method is used as a decorator. Please explicitly assign a bool value to this argument when using this method in a Python Shell. If not specified, the method will try to determine it automatically.
- Returns:
The opened file object.
- Return type:
file object