Spreadsheets as data: spreadsheet handling for humans
Project description
What is this?
Don’t be sad, turn that messy spreadsheet into actual data! This is for people new to programming who need to do stuff with typical messy spreadsheet data.
It’s also for anyone who could use quick clean access to the kind of spreadsheet data passed around in offices.
It’s not for data scientists and huge data sets (fortunately there’s plenty of support for that elsewhere).
How to install
pip3 install sad
How to access the spreadsheet
These examples assume a simple spreadsheet file called hr.csv that contains these values:
Full Name Employee ID Start Date Sue Storm 1000 1/1/18 Kitty Pride 1001 2/5/18
Use this import statement:
from sad import Spreadsheet
The spreadsheet data needs to be in CSV format. Open the file and pass the file object to Spreadsheet.from_csv():
hr_csv = open('hr.csv') sheet = Spreadsheet.from_csv(hr_csv)
Accessing cells by index
The Spreadsheet can be treated as a 2-dimensional array, so if you want the value of the second cell down and third cell over, you can do this:
value = sheet[1][2] print(value)
This prints:
1/1/18
Accessing cells by header name
If you use a header name to get to a cell, the first row of the spreadsheet is assumed to be the headers. To get the same cell as above, you can do this:
value = sheet[1]['Start Date'] print(value)
This also prints:
1/1/18
You may find it more intuitive to get the row first, like so:
employee = sheet[1] print(employee['Start Date']
Even better, you can use header names for both the row and column:
print(sheet['Sue Storm']['Start Date'])
Looping over rows
It’s easy to loop over rows:
for row in sheet: print(row)
This prints:
First Name, Last name, start date Sue, Storm, 1/1/18 Kitty, Pride, 2/5/18
To print every cell in the spreadsheet:
for row in sheet: for cell in row: print(cell)
This prints:
Full Name Employee ID Start Date Sue Storm 1/1/18 Kitty Pride 2/5/18
Looping over columns
You can also loop over columns:
for col in sheet.columns: print(col) print('----')
This prints:
Full Name Sue Storm Kitty Pride ---- Employee ID 1000 1001 ---- Start Date 1/1/18 2/5/18
What’s next?
This first release provides some nice intuitive ways to access spreadsheet data.
Future releases will add support for dealing with typical problems in spreadsheets that were created for humans to read rather than for computers to process.
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.