Skip to content

Module: micpy.tab

The micpy.tab module provides methods to read and parse tabular files.

FormatError

Bases: Exception

Raised when there's a problem with the format of the tabular file.

Source code in micpy\tab.py
class FormatError(Exception):
    """Raised when there's a problem with the format of the tabular file."""

parse(string, parse_header=True, ignore_invalid_header=True)

Parse a tabular file and return the content as a pandas DataFrame.

Parameters:

Name Type Description Default
string str

The content of a tabular file as a string.

required
parse_header bool

Whether to parse the header. Defaults to True.

True
ignore_invalid_header bool

Whether to ignore invalid headers. Defaults to True.

True

Raises:

Type Description
`FormatError`

If the number of columns in the header does not match the body.

Returns:

Type Description
DataFrame

The content of the file as a pandas DataFrame.

Source code in micpy\tab.py
def parse(
    string: str, parse_header: bool = True, ignore_invalid_header: bool = True
) -> DataFrame:
    """Parse a tabular file and return the content as a pandas DataFrame.

    Args:
        string (str): The content of a tabular file as a string.
        parse_header (bool, optional): Whether to parse the header. Defaults to `True`.
        ignore_invalid_header (bool, optional): Whether to ignore invalid headers.
            Defaults to `True`.

    Raises:
        `FormatError`: If the number of columns in the header does not match the body.

    Returns:
        The content of the file as a pandas DataFrame.
    """
    lines = string.splitlines()
    return _parse_lines(lines, parse_header, ignore_invalid_header)

read(filename, parse_header=True, ignore_invalid_header=True)

Read a tabular file and return the content as a pandas DataFrame.

Parameters:

Name Type Description Default
filename str

Path to the file.

required
parse_header bool

Whether to parse the header. Defaults to True.

True
ignore_invalid_header bool

Whether to ignore invalid headers. Defaults to True.

True

Raises:

Type Description
`FormatError`

If the number of columns in the header does not match the body.

Returns:

Type Description
DataFrame

The content of the file as a pandas DataFrame.

Source code in micpy\tab.py
def read(
    filename: str, parse_header: bool = True, ignore_invalid_header: bool = True
) -> DataFrame:
    """Read a tabular file and return the content as a pandas DataFrame.

    Args:
        filename (str): Path to the file.
        parse_header (bool, optional): Whether to parse the header. Defaults to `True`.
        ignore_invalid_header (bool, optional): Whether to ignore invalid headers.
            Defaults to `True`.

    Raises:
        `FormatError`: If the number of columns in the header does not match the body.

    Returns:
        The content of the file as a pandas DataFrame.
    """
    with open(filename, mode="r", encoding="utf8") as file:
        lines = file.readlines()
    return _parse_lines(lines, parse_header, ignore_invalid_header)