Skip to content

Latest commit

Β 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Š datacsv – Lightweight CSV Database in Pure Python

A minimalist, zero-dependency, file-based CSV database for local Python automation.

datacsv demo


πŸ“¦ What is datacsv?

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.


πŸ”§ Features

  • βœ… 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

πŸ“ˆ Common Use Cases

  • βœ… 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

πŸ’» Installation

# Download the single Python file
git clone https://github.com/mvish77/datacsv.git
cd datacsv

🐍 Usage in Python

1. Create a new CSV database

from datacsv import CSVDatabase

db = CSVDatabase('users.csv', ['id', 'name', 'email'])
db.insert({'id': 1, 'name': 'Alice', 'email': 'alice@example.com'})
print(db.find_all())

1. OR Load existing CSV

from datacsv import CSVDatabase

db = CSVDatabase('users.csv')  # Automatically loads headers
print(db.find_all())

🧠 API Reference – All Functions with Examples

Here are the core methods provided by CSVDatabase, along with their usage.

1. insert(data: dict)

Inserts a new row into the CSV.

db.insert({'id': 1, 'name': 'Alice', 'email': 'alice@example.com'})

2. find(field: str, value: Any) β†’ dict or None

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'}

3. find_all(field: str) β†’ List[Any]

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']

4. find_where(condition: Callable[[dict], bool]) β†’ List[dict]

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', ...}]

5. update(key,value, new_data: dict)

Updates rows where a field matches the value.

db.update('id',1,{'name': 'Alicia'})

6. update_where(condition: Callable[[dict], bool], new_data: dict)

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', ...}]

7. delete(key, value)

Deletes all rows where field == value.

db.delete('id',1)

8. delete_where(condition: Callable[[dict], bool])

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 False

9. delete_db()

Permanently deletes the CSV file from disk.

db.delete_db()

πŸ“€ Export Methods

Methods to export or print database in JSON or HTML format

1. to_json(indent: int = 4)

Exports the entire CSV content as JSON string.

json_output = db.to_json()
print(json_output)

2. to_html(table_class:str)

html_output = db.to_html()
print(html_output)

πŸš€ Future Enhancements

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)

Feel free to suggest more by opening an issue!


🀝 Contributions Welcome!

Your contributions are welcome to make this project even better.

To contribute:

  1. Fork the repository
  2. Create a new branch (git checkout -b feature/some-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin feature/some-feature)
  5. Create a new Pull Request

If you're fixing bugs or enhancing features, include relevant tests.

πŸ“Œ Badge & Visual

DataCSV

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.


πŸ“œ License

MIT license

About

A lightweight, zero-dependency, file-based database system in Python using CSV as the database storage. Help to create file based database easily.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages