espnet2.fileio.read_text.load_num_sequence_text
Less than 1 minute
espnet2.fileio.read_text.load_num_sequence_text
espnet2.fileio.read_text.load_num_sequence_text(path: Path | str, loader_type: str = 'csv_int') → Dict[str, List[float | int]]
Load a text file indicating sequences of numbers.
This function reads a text file where each line contains a key followed by a sequence of numbers. The numbers can be either integers or floats depending on the specified loader type. The function returns a dictionary where the keys are the first elements of each line and the values are lists of numbers parsed from the corresponding lines.
- Parameters:
- path (Union *[*Path , str ]) – The path to the text file to be read.
- loader_type (str) – The format of the numbers in the file. Supported values are:
- “text_int”: Space-separated integers
- “text_float”: Space-separated floats
- “csv_int”: Comma-separated integers
- “csv_float”: Comma-separated floats
- Returns: A dictionary mapping keys to lists of numbers parsed from the text file.
- Return type: Dict[str, List[Union[float, int]]]
- Raises:ValueError – If an unsupported loader_type is specified.
Examples
Assuming the content of ‘text’ file is: : key1 1 2 3 key2 34 5 6
>>> d = load_num_sequence_text('text')
>>> print(d)
{'key1': [1, 2, 3], 'key2': [34, 5, 6]}
For floating point numbers: Assuming the content of ‘text_float’ file is:
key1 1.0 2.5 3.3 key2 34.1 5.6 6.2
>>> d = load_num_sequence_text('text_float', loader_type='text_float')
>>> print(d)
{'key1': [1.0, 2.5, 3.3], 'key2': [34.1, 5.6, 6.2]}