stouputils.io.csv module#
- csv_dump(
- data: Any,
- file: IO[Any] | str | None = None,
- delimiter: str = ',',
- has_header: bool = True,
- index: bool = False,
- *args: Any,
- **kwargs: Any,
Writes data to a CSV file with customizable options and returns the CSV content as a string.
- Parameters:
data (list[list[Any]] | list[dict[str, Any]] | pd.DataFrame | pl.DataFrame) – The data to write, either a list of lists, list of dicts, pandas DataFrame, or Polars DataFrame
file (IO[Any] | str) – The file object or path to dump the data to
delimiter (str) – The delimiter to use (default: ‘,’)
has_header (bool) – Whether to include headers (default: True, applies to dict and DataFrame data)
index (bool) – Whether to include the index (default: False, only applies to pandas DataFrame)
*args (Any) – Additional positional arguments to pass to the underlying CSV writer or DataFrame method
**kwargs (Any) – Additional keyword arguments to pass to the underlying CSV writer or DataFrame method
- Returns:
The CSV content as a string
- Return type:
str
Examples
>>> csv_dump([["a", "b", "c"], [1, 2, 3], [4, 5, 6]]) 'a,b,c\r\n1,2,3\r\n4,5,6\r\n'
>>> csv_dump([{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]) 'name,age\r\nAlice,30\r\nBob,25\r\n'
- csv_load(
- file_path: str,
- delimiter: str = ',',
- has_header: bool = True,
- as_dict: bool = False,
- as_dataframe: bool = False,
- use_polars: bool = False,
- *args: Any,
- **kwargs: Any,
- csv_load(
- file_path: str,
- delimiter: str = ',',
- has_header: bool = True,
- *,
- as_dict: Literal[True],
- as_dataframe: bool = False,
- use_polars: bool = False,
- **kwargs: Any,
- csv_load(
- file_path: str,
- delimiter: str = ',',
- has_header: bool = True,
- as_dict: bool = False,
- *,
- as_dataframe: Literal[True],
- use_polars: Literal[False] = False,
- **kwargs: Any,
- csv_load(
- file_path: str,
- delimiter: str = ',',
- has_header: bool = True,
- as_dict: bool = False,
- *,
- as_dataframe: Literal[True],
- use_polars: Literal[True],
- **kwargs: Any,
Load a CSV file from the given path
- Parameters:
file_path (str) – The path to the CSV file
delimiter (str) – The delimiter used in the CSV (default: ‘,’)
has_header (bool) – Whether the CSV has a header row (default: True)
as_dict (bool) – Whether to return data as list of dicts (default: False)
as_dataframe (bool) – Whether to return data as a DataFrame (default: False)
use_polars (bool) – Whether to use Polars instead of pandas for DataFrame (default: False, requires polars)
*args – Additional positional arguments to pass to the underlying CSV reader or DataFrame method
**kwargs – Additional keyword arguments to pass to the underlying CSV reader or DataFrame method
- Returns:
The content of the CSV file
- Return type:
list[list[str]] | list[dict[str, str]] | pd.DataFrame | pl.DataFrame
Examples
> Assuming "test.csv" contains: a,b,c\n1,2,3\n4,5,6 > csv_load("test.csv") [['1', '2', '3'], ['4', '5', '6']] > csv_load("test.csv", as_dict=True) [{'a': '1', 'b': '2', 'c': '3'}, {'a': '4', 'b': '5', 'c': '6'}] > csv_load("test.csv", as_dataframe=True) a b c 0 1 2 3 1 4 5 6
> csv_load("test.csv", as_dataframe=True, use_polars=True) shape: (2, 3) ┌─────┬─────┬─────┐ │ a ┆ b ┆ c │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═════╪═════╪═════╡ │ 1 ┆ 2 ┆ 3 │ │ 4 ┆ 5 ┆ 6 │ └─────┴─────┴─────┘