Easy CSV
py_simple.easy_csv
filter_csv_rows(filepath, column, value, return_dict=True, delimiter=',')
Filter rows where a specific column equals the given value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the CSV file. |
required |
column
|
str
|
Column name to filter on. |
required |
value
|
str
|
Value to match. |
required |
return_dict
|
bool
|
Whether to return dicts or lists (see read_csv_to_list). |
True
|
delimiter
|
str
|
Field delimiter (default is comma). |
','
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list[dict[str, Any]] | list[list[Any]]
|
Filtered rows (dicts or lists). |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If filepath doesn't exist. |
ValueError
|
If the column is not found. |
Example
from py_simple import filter_csv_rows
data = filter_csv_rows(filepath="people.csv", column="Name", value="Alice")
print(data) # [{'Name': 'Alice', 'Age': '24'}]
import csv
with open("people.csv", "r", newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
data = [row for row in reader if row["Name"] == "Alice"]
print(data) # [{'Name': 'Alice', 'Age': '24'}]
get_csv_columns(filepath, delimiter=',')
Retrieve a CSV column names (headers) from a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the CSV file. |
required |
delimiter
|
str
|
Field delimiter (default is comma). |
','
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list[str]
|
Column names. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If filepath doesn't exist. |
ValueError
|
If the file is empty. |
Example
from py_simple import get_csv_columns
columns = get_csv_columns(filepath="people.csv")
print(columns) # ['Name', 'Age']
import csv
with open("people.csv", "r", newline="", encoding="utf-8") as f:
reader = csv.reader(f)
columns = next(reader)
print(columns) # ['Name', 'Age']
read_csv_to_list(filepath, return_dict=True, delimiter=',')
Read a CSV file and return its contents Args: filepath (str): The path to the CSV file. return_dict (bool): If True, return list of dicts (keys are headers). delimiter (str): Field delimiter (default is comma).
Returns:
| Name | Type | Description |
|---|---|---|
list |
list[dict[str, Any]] | list[list[Any]]
|
Rows as dicts (if return_dict=True) or lists. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If filepath doesn't exist. |
ValueError
|
If the file is empty. |
Example
from py_simple import read_csv_to_list
data = read_csv_to_list(filepath="people.csv")
print(data[0]) # {'Name': 'Alice', 'Age': '24'}
rows = read_csv_to_list(filepath="people.csv", return_dict=False)
print(rows[0]) # ['Alice','24']
import csv
with open("people.csv", "r", newline="", encoding="utf-8") as f:
reader = csv.reader(f)
rows = list(reader)
headers = rows[0]
data = [dict(zip(headers, row)) for row in rows[1:]]
print(data[0]) # {'Name': 'Alice', 'Age': '24'}
with open("people.csv", "r", newline="", encoding="utf-8") as f:
reader = csv.reader(f)
rows = list(reader)
print(rows[1]) # ['Alice', '24'] # 0 is a header row
write_csv_from_list(filepath, data, headers=None, delimiter=',')
Write data to a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Output path. |
required |
data
|
list
|
List of dicts or list of lists. |
required |
headers
|
list
|
Column names. Required if data is list of lists and you want headers. If data is dict, keys are used. |
None
|
delimiter
|
str
|
Field delimiter (default is comma). |
','
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If data is empty or invalid. |
Example
from py_simple import write_csv_from_list
people = [
{"Name": "Alice", "Age": "24"},
{"Name": "Bob", "Age": "31"},
]
write_csv_from_list(filepath="people.csv", data=people)
import csv
people = [
{"Name": "Alice", "Age": "24"},
{"Name": "Bob", "Age": "31"},
]
headers = list(people[0].keys())
with open("people.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=headers)
writer.writeheader()
writer.writerows(people)