Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
A simple XLSX/CSV reader - to dictionary converter
To install the package from pip, first run:
python3 -m pip install --no-cache-dir sheet2dict
Required pip packages for sheet2doc: csv, openpyxl
This library has 2 main features: reading a spreadsheet files and converting them to array of python dictionaries.
Use xlsx_to_dict()
method when converting form spreadsheets.
Supported file formats for spreadsheets are: .xlsx,.xlsm,.xltx,.xltm
Spreadsheets with multiple worksheets are supported. If no sheet is specified, the active sheet is selected. If there is only one sheet, it is considered active.
# Import the library
from sheet2dict import Worksheet
# Create an object
ws = Worksheet()
# Convert active sheet (without specifying sheet name)
ws.xlsx_to_dict(path='inventory.xlsx')
# Convert the 'Main Warehouse' sheet of the 'inventory.xslx' spreadsheet file.
ws.xlsx_to_dict(path='inventory.xlsx', select_sheet='Main Warehouse')
# 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)
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)
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'}
]
As an open source project, sheet2dict welcomes contributions of many forms.
Please read and follow our Contributing to sheet2dict
Contributors:
As a contributor, you can help us keep the sheet2dict project open and inclusive.
Please read and follow our Code of Conduct
FAQs
Simple XLSX and CSV to dictionary converter
We found that sheet2dict demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.