csvio: Python Library for processing CSV files
csvio is a Python library that provides a wrapper around Python’s built in
csv.DictReader
and csv.DictWriter
, for ease of reading and
writing CSV files.
Rows in a CSV are represented and processed as a list of dictionaries. Each item in this list is a dictionary that represents a row. The key, value pairs in each dictionary is a mapping between the column and its associated row value from the CSV.
Installation
pip install csvio
Documentation
Reading CSVs
CSV file contents:
Supplier,Fruit,Quantity
Big Apple,Apple,1
Big Melons,Melons,2
Long Mangoes,Mango,3
Small Strawberries,Strawberry,4
>>> from csvio import CSVReader
>>> reader = CSVReader("fruit_stock.csv")
>>> reader.fieldnames
['Supplier', 'Fruit', 'Quantity']
>>> len(reader.rows)
4
>>> import json
>>> print(json.dumps(reader.rows, indent=4))
[
{
"Supplier": "Big Apple",
"Fruit": "Apple",
"Quantity": "1"
},
{
"Supplier": "Big Melons",
"Fruit": "Melons",
"Quantity": "2"
},
{
"Supplier": "Long Mangoes",
"Fruit": "Mango",
"Quantity": "3"
},
{
"Supplier": "Small Strawberries",
"Fruit": "Strawberry",
"Quantity": "4"
}
]
Writing CSVs
>>> from csvio import CSVWriter
>>> writer = CSVWriter("fruit_stock.csv", fieldnames=["Supplier", "Fruit", "Quantity"])
>>> row1 = {"Supplier": "Big Apple", "Fruit": "Apple", "Quantity": 1}
>>> writer.add_rows(row1)
>>> rows2_3_4 = [
... {"Supplier": "Big Melons", "Fruit": "Melons", "Quantity": 2},
... {"Supplier": "Long Mangoes", "Fruit": "Mango", "Quantity": 3},
... {"Supplier": "Small Strawberries", "Fruit": "Strawberry", "Quantity": 4}
... ]
>>> writer.add_rows(rows2_3_4)
>>> len(writer.pending_rows)
4
>>> len(writer.rows)
0
>>> writer.flush()
>>> len(writer.pending_rows)
0
>>> len(writer.rows)
4
Once flush is called a CSV file with the name fruit_stock.csv will be written with the following contents.
Supplier,Fruit,Quantity
Big Apple,Apple,1
Big Melons,Melons,2
Long Mangoes,Mango,3
Small Strawberries,Strawberry,4
Apply processors to transform row values
Create nested dictionaries with specified path
CSV Contents: fruit_stock.csv
Supplier,Fruit,Origin,Quantity
Big Apples,Apple,Spain,1
Big Melons,Melons,Italy,2
Long Mangoes,Mango,India,3
Small Strawberries,Strawberry,France,4
Short Mangoes,Mango,France,5
Sweet Strawberries,Strawberry,Spain,6
Square Apples,Apple,Italy,7
Small Melons,Melons,Italy,8
Dark Berries,Strawberry,Australia,9
Sweet Berries,Blackcurrant,Australia,10
Create dictionary with hierarchy {"Fruit": [rows]}
from csvio.csvreader import CSVReader
from json import dumps
reader = CSVReader("fruit_stock.csv")
col_order = ["Fruit"]
dict_tree= reader.rows_to_nested_dicts(col_order)
print(dumps(dict_tree, indent=4))
Output:
{
"Apple": [
{
"Supplier": "Big Apples",
"Fruit": "Apple",
"Origin": "Spain",
"Quantity": "1"
},
{
"Supplier": "Square Apples",
"Fruit": "Apple",
"Origin": "Italy",
"Quantity": "7"
}
],
"Melons": [
{
"Supplier": "Big Melons",
"Fruit": "Melons",
"Origin": "Italy",
"Quantity": "2"
},
{
"Supplier": "Small Melons",
"Fruit": "Melons",
"Origin": "Italy",
"Quantity": "8"
}
],
"Mango": [
{
"Supplier": "Long Mangoes",
"Fruit": "Mango",
"Origin": "India",
"Quantity": "3"
},
{
"Supplier": "Short Mangoes",
"Fruit": "Mango",
"Origin": "France",
"Quantity": "5"
}
],
"Strawberry": [
{
"Supplier": "Small Strawberries",
"Fruit": "Strawberry",
"Origin": "France",
"Quantity": "4"
},
{
"Supplier": "Sweet Strawberries",
"Fruit": "Strawberry",
"Origin": "Spain",
"Quantity": "6"
},
{
"Supplier": "Dark Berries",
"Fruit": "Strawberry",
"Origin": "Australia",
"Quantity": "9"
}
],
"Blackcurrant": [
{
"Supplier": "Sweet Berries",
"Fruit": "Blackcurrant",
"Origin": "Australia",
"Quantity": "10"
}
]
}
Create dictionary with hierarchy {"Fruit": "Origin" : [rows]}
from csvio.csvreader import CSVReader
from json import dumps
reader = CSVReader("fruit_stock.csv")
col_order = ["Fruit", "Origin"]
dict_tree= reader.rows_to_nested_dicts(col_order)
print(dumps(dict_tree, indent=4))
Output:
{
"Apple": {
"Spain": [
{
"Supplier": "Big Apples",
"Fruit": "Apple",
"Origin": "Spain",
"Quantity": "1"
}
],
"Italy": [
{
"Supplier": "Square Apples",
"Fruit": "Apple",
"Origin": "Italy",
"Quantity": "7"
}
]
},
"Melons": {
"Italy": [
{
"Supplier": "Big Melons",
"Fruit": "Melons",
"Origin": "Italy",
"Quantity": "2"
},
{
"Supplier": "Small Melons",
"Fruit": "Melons",
"Origin": "Italy",
"Quantity": "8"
}
]
},
"Mango": {
"India": [
{
"Supplier": "Long Mangoes",
"Fruit": "Mango",
"Origin": "India",
"Quantity": "3"
}
],
"France": [
{
"Supplier": "Short Mangoes",
"Fruit": "Mango",
"Origin": "France",
"Quantity": "5"
}
]
},
"Strawberry": {
"France": [
{
"Supplier": "Small Strawberries",
"Fruit": "Strawberry",
"Origin": "France",
"Quantity": "4"
}
],
"Spain": [
{
"Supplier": "Sweet Strawberries",
"Fruit": "Strawberry",
"Origin": "Spain",
"Quantity": "6"
}
],
"Australia": [
{
"Supplier": "Dark Berries",
"Fruit": "Strawberry",
"Origin": "Australia",
"Quantity": "9"
}
]
},
"Blackcurrant": {
"Australia": [
{
"Supplier": "Sweet Berries",
"Fruit": "Blackcurrant",
"Origin": "Australia",
"Quantity": "10"
}
]
}
}
Construct a dictionary with number of rows for each unique Origin
from csvio.csvreader import CSVReader
from json import dumps
reader = CSVReader("fruit_stock.csv")
col_order = ["Origin"]
origin_fruit_count = {}
dict_tree = reader.rows_to_nested_dicts(col_order)
for origin in dict_tree:
origin_fruit_count.setdefault(origin, len(dict_tree[origin]))
print(dumps(origin_fruit_count, indent=4))
Output:
{
"Spain": 2,
"Italy": 3,
"India": 1,
"France": 2,
"Australia": 2
}