Easy CSV
py_simple.easy_csv
easy_csv is built to simplify reading and writing CSV files.
append_row_to_csv(filepath, row, delimiter=',')
Append a single row (as a dictionary or list) to an existing CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the CSV file. |
required |
row
|
dict or list
|
The row data to append. |
required |
delimiter
|
str
|
Field delimiter (default is comma). |
','
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If filepath doesn't exist. |
ValueError
|
If the file is empty. |
count_csv_rows(filepath, include_header=False, delimiter=',')
Count rows in a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the CSV file. |
required |
include_header
|
bool
|
If True, include the header row in the count. |
False
|
delimiter
|
str
|
Field delimiter (default is comma). |
','
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of rows in the CSV file. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If filepath doesn't exist. |
ValueError
|
If the file is empty. |
Example
from py_simple import count_csv_rows
row_count = count_csv_rows(filepath="people.csv")
print(row_count) # 3
import csv
with open("people.csv", "r", newline="", encoding="utf-8") as f:
row_count = sum(1 for _ in csv.reader(f)) - 1
print(row_count) # 3
delete_csv_rows(filepath, column, value, delimiter=',')
Deletes rows from a CSV file where a column matches the given value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
The path to the CSV file. |
required |
column
|
str
|
The column to check for the given value. |
required |
value
|
str
|
The value used to find rows to delete. |
required |
delimiter
|
str
|
The field delimiter. Defaults to a comma. |
','
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the CSV file does not exist. |
ValueError
|
If the file is empty, the column does not exist, or no row matches the given value. |
Example
from py_simple import delete_csv_rows
delete_csv_rows(
filepath="people.csv",
column="Name",
value="Alice"
)
import csv
import os
import tempfile
with open("people.csv", "r", newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
rows = list(reader)
rows = [row for row in rows if row["Name"] != "Alice"]
with open("people.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=reader.fieldnames)
writer.writeheader()
writer.writerows(rows)
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_column(filepath=None, column=None, delimiter=',', *, file_path=None, column_name=None)
Read all values from a specific column in a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the CSV file. Also accepts file_path as alias. |
None
|
column
|
str
|
Column name to extract. Also accepts column_name as alias. |
None
|
delimiter
|
str
|
Field delimiter (default is comma). |
','
|
file_path
|
str
|
Keyword alias for filepath. |
None
|
column_name
|
str
|
Keyword alias for column. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list[str]
|
Values from the specified column. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If filepath doesn't exist. |
ValueError
|
If the file is empty or if the column is not found. |
Example
from py_simple import read_csv_column
names = read_csv_column(filepath="people.csv", column="Name")
print(names) # ['Alice', 'Bob', 'Carol']
import csv
with open("people.csv", "r", newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
names = [row["Name"] for row in reader]
print(names) # ['Alice', 'Bob', 'Carol']
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)