Simple XLSX and CSV to dictionary converter
Project description
sheet2dict
A simple XLSX/CSV to dictionary converter.
Installing
To install the package from pip, first run:
python3 -m pip install --no-cache-dir sheet2dict
Required pip packages for sheet2doc: csv, openpyxl
Usage
This library has 2 main features: reading a spreadsheet files and converting them to array of python dictionaries.
- XLSX
Use xlsx_to_dict()
method when converting form spreadsheets.
Supported file formats for spreadsheets are: .xlsx,.xlsm,.xltx,.xltm
# Import the library
from sheet2dict import Worksheet
# Create an object
ws = Worksheet()
# Convert
ws.xlsx_to_dict(path='inventory.xlsx')
# object.header returns first row with the data in a spreadsheet
print(ws.header)
# object.sheet_items returns converted rows as dictionaries in the array
print(ws.sheet_items)
You can parse data when worksheet is an object
# Import the library
from sheet2dict import Worksheet
# Example: read spreadsheet as object
path = 'inventory.xlsx'
xlsx_file = open(path, 'rb')
xlsx_file = BytesIO(xlsx_file.read())
# Parse spreadsheet from object
ws = Worksheet()
ws.xlsx_to_dict(path=xlsx_file)
print(ws.header)
- CSV
Use csv_to_dict()
method when converting form csv.
CSV is a format with many variations, better handle encodings and delimiters on user side and not within module itself.
# Import the library
from sheet2dict import Worksheet
# Create an object
ws = Worksheet()
# Read CSV file
csv_file = open('inventory.csv', 'r', encoding='utf-8-sig')
# Convert
ws.csv_to_dict(csv_file=csv_file, delimiter=';')
# object.header returns first row with the data in a spreadsheet
print(ws.header)
# object.sheet_items returns converted rows as dictionaries in the array
print(ws.sheet_items)
- Other functions
Worksheet object.header returns first row with the data in a spreadsheet
Python 3.9.1
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
>>> from sheet2dict import Worksheet
>>> ws = Worksheet()
>>> ws.xlsx_to_dict(path="inventory.xlsx")
>>> ws.header
{'country': 'SK', 'city': 'Bratislava', 'citizens': '400000', 'random_field': 'cc'}
Worksheet object.sanitize_sheet_items removes None or empty dictionary keys from sheet_items
>>> from sheet2dict import Worksheet
>>> ws = Worksheet()
>>> ws.xlsx_to_dict(path="inventory.xlsx")
>>> ws.sheet_items
[
{'country': 'CZ', 'city': 'Prague', 'citizens': '600000', None: '22', 'random_field': 'cc'},
{'country': 'UK', 'city': 'London', 'citizens': '2000000', None: '33', 'random_field': 'cc'}
]
>>> ws.sanitize_sheet_items
[
{'country': 'CZ', 'city': 'Prague', 'citizens': '600000', 'random_field': 'cc'},
{'country': 'UK', 'city': 'London', 'citizens': '2000000', 'random_field': 'cc'}
]
Project details
Release history Release notifications | RSS feed
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
Hashes for sheet2dict-0.0.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24aadbf40d72b2bff03196b97d8d3cd9ddd61e84127d979d8929c11e039afc96 |
|
MD5 | 1e92f57528665aeb1ea47ac0552d22aa |
|
BLAKE2b-256 | e581d4b9673e94742304fe647fbb2daff0e4874c84015e2e4cf6611ec79a884a |