A minimalist, zero-dependency, file-based CSV database for local Python automation.
datacsv is a pure Python utility that lets you treat CSV files like simple databases. No need for Pandas, SQLite, or Excel. Just plug in your CSV File and get fast, safe read/write operations β directly from Python script.
- β A very Lightweight, no dependencies, beginner-friendly
- β Auto-casts types: strings, integers, floats, booleans
- β Insert, update, delete rows like a database
- β Query/filter/search with ease
- β Advance searching with function pass
- β Clean and simple python methods to perform operations
- β JSON & HTML export with indentation support
- β Custom error handling and type safety
- β Maintain a local CSV-based "database"
- β Build lightweight CLI tools
- β Prototype data models quickly without installing SQL
- β Store and export user logs or events
- β Analyze data with filters and conditions
- β Share flat file databases easily across environments
- β Store server logs according to different userbase
- β It also helpful to create blog post with no database setup
- β You can create multipage csv database just by creating its object
# Download the single Python file
git clone https://github.com/mvish77/datacsv.git
cd datacsvfrom datacsv import CSVDatabase
db = CSVDatabase('users.csv', ['id', 'name', 'email'])
db.insert({'id': 1, 'name': 'Alice', 'email': 'alice@example.com'})
print(db.find_all())from datacsv import CSVDatabase
db = CSVDatabase('users.csv') # Automatically loads headers
print(db.find_all())Here are the core methods provided by CSVDatabase, along with their usage.
Inserts a new row into the CSV.
db.insert({'id': 1, 'name': 'Alice', 'email': 'alice@example.com'})Returns the first row where the field matches the given value.
result = db.find('id', 1)
print(result) # {'id': 1, 'name': 'Alice', 'email': 'alice@example.com'}Returns a list of all values from the specified column.
all = db.find_all() # return everything from database in list
emails = db.find_all('email') # return only specific key values
print(emails) # ['alice@example.com', 'bob@example.com']Returns all rows where the condition returns True.
results = db.find_where(lambda row: row['name'].startswith('A'))
print(results) # [{'id': 1, 'name': 'Alice', ...}]OR
def gt_id(row):
return row['id'] > 5
results = db.find_where(gt_id)
print(results) # [{'id': 1, 'name': 'Alice', ...}]Updates rows where a field matches the value.
db.update('id',1,{'name': 'Alicia'})Updates all rows where condition returns True, replacing fields with new_data.
db.update_where(lambda row: row['name'].startswith('B'), {'email': 'bob@newmail.com'})OR
def gt_name(row):
return row['name'].startswith('B')
results = db.update_where(gt_name)
print(results) # [{'id': 2, 'name': 'Bob', ...}]Deletes all rows where field == value.
db.delete('id',1)Deletes all rows where the condition returns True.
def gt_name(row):
return row['name'].startswith('B')
db.delete_where(gt_name) # return True else FalsePermanently deletes the CSV file from disk.
db.delete_db()Methods to export or print database in JSON or HTML format
Exports the entire CSV content as JSON string.
json_output = db.to_json()
print(json_output)html_output = db.to_html()
print(html_output)Here are some features planned for future versions:
- β Type-safe schema validation for rows
- β Auto-generate unique IDs for primary key fields
- β Indexing support for faster reads on large files
- β Date/time field parsing and conversion
- β Built-in CSV to SQLite converter
- β Import/export to Excel (XLSX)
Your contributions are welcome to make this project even better.
- Fork the repository
- Create a new branch (
git checkout -b feature/some-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin feature/some-feature) - Create a new Pull Request
If you're fixing bugs or enhancing features, include relevant tests.
A minimal Python class to manage CSV files like a lightweight database.
Ideal for prototyping, quick CLI tools, and managing structured flat data with ease.
MIT license