easyio
A set of tools to handle file input/output like excel, txt, csv, etc.
Read excel
Assume you have a xlsx file like this:
Sheet1
| id | Name | Age | Description |
|---|---|---|---|
| 1 | Peter | 45 | A man |
| 2 | Brain | 22 | A foo |
Sheet2
| Book | Price | Location |
|---|---|---|
| To kill a mocking bird | $9.9 | Level-2-rack-1 |
| Python cookbook | $12.9 | Level-1-rack-2 |
- Then if you want to read all sheets:
from easyio import ExcelReader
# Path to xlsx file
xlsx_file_path = 'easyio/excel/tests/test.xlsx'
reader = ExcelReader(xlsx_file_path)
# Print all sheets rows
with reader:
for sheet, rows_g in reader.read_sheets(values_only=True):
print(f'sheet: {sheet}')
for row in rows_g:
print(row)
- Output:
sheet: Sheet1
('id', 'Name', 'Age', 'Description')
(1, 'Peter', 45, 'A man')
(2, 'Brain', 22, 'A fool')
sheet: Sheet2
('Book', 'Price', 'Location')
('To kill a mocking bird', '$9.9', 'Level-2-rack-1')
('Python cookbook', '$12.9', 'Level-1-rack-2')
- If you want to print specific sheet:
with reader:
for row in reader.read_sheet('Sheet1'):
print(row)
- Output:
('id', 'Name', 'Age', 'Description')
(1, 'Peter', 45, 'A man')
(2, 'Brain', 22, 'A fool')
- Specific the col you want to read:
with reader:
for row in reader.read_sheet('Sheet1', cols=['id', 'Name']):
print(row)
# You can also specify cols by str like:
for row in reader.read_sheet('Sheet1', cols='id,Name'):
...
# or with range(1-based, 1:3 means from column 1 to column 2):
for row in reader.read_sheet('Sheet1', cols='1:3'):
...
# or just specify the column index(1-based)
for row in reader.read_sheet('Sheet1', copls='1,2'):
...
- Output:
('id', 'Name')
(1, 'Peter')
(2, 'Brain')
Sometime, you do not want to read a xlsx-file with a list of tuples returned, you may want to get a list of dicts.
- Return rows as list[dict] format
with reader:
for row in reader.read_sheet_as_dict():
print(row)
- Output:
{'id': 1,'Name': 'Peter', 'Age': 45, 'Description': 'A man'}
......
- You can also specify the headers
with reader:
for row in reader.read_sheet_as_dict(
header={
'id': 'id',
'Name': 'user_name',
'Age': 'user_age',
'Description': 'desc'
}
):
print(row)
- Output:
{'id': 1, 'user_name': 'Peter', 'user_age': 45, 'desc': 'A man'}
-
There are 3 ways to specify header when you want to return a list of dicts:
- None: If header is None, the first row will be regarded as header
- list[str]: A list of string. eg:
header = ['id', '', 'Name']will read column1 and column3, and ignore column 2 - dict[str, str]: A map of xlsx-file-header: header-you-want. eg:
header = {'Name':'user_name'}will only read columnNameand return with {user_name: ...}
Metadata
Release files for py-easyio 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py-easyio-0.0.2.tar.gz | 12.8 kB | Without provenance | Details for py-easyio-0.0.2.tar.gz |
Release files / py-easyio-0.0.2.tar.gz
| Download URL | py-easyio-0.0.2.tar.gz |
|---|---|
| Size | 12.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
51d8734cba137225d9dd16b57a9f1a2895699b6f315f533f3fd86a4ab798a5cc
|
|
BLAKE2b-256 checksum How to use checksums |
0a18317faf34665a2566cd2f57297d333e730e6eab63c2ca83305601901ae354
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.4
|
Security for py-easyio 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.