CSV Wrapper for conveniently processing csv files
Project description
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file csvio-1.1.2.tar.gz.
File metadata
- Download URL: csvio-1.1.2.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.8.2 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6191c336301015cac8a0c8ce773de8a273123ae59cd35fed2eb69c6937c4ca2
|
|
| MD5 |
c5f825322de48d64f28f2a5574345c9d
|
|
| BLAKE2b-256 |
91cf39e36b59cbcdbcf6a091bc2fc20d61acaa82f39dbbfa6e9367f58cb4433d
|
File details
Details for the file csvio-1.1.2-py3-none-any.whl.
File metadata
- Download URL: csvio-1.1.2-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.8.2 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d805ad80a45ad24ef2ce766c420e4acde3015c654eb59df1c2463da787b0b1da
|
|
| MD5 |
c7d108a91c0c1a7359590e37802835de
|
|
| BLAKE2b-256 |
6c267b59084d4a8ecf0240496a4ddeb9154b5930e6f2810007581cae414250c8
|