DEV Community

Cover image for How to Parse a Spreadsheet in Python

How to Parse a Spreadsheet in Python

Jeremy Grifski on March 04, 2019

Every once in awhile, I’ll have the need to load data from a spreadsheet into a Python program, but one question always comes up: what’s the best w...
Collapse
 
hamatti profile image
Juha-Matti Santala

One of the main benefits for using the csv library in Python is that it handles edge cases much more gracefully than a custom and naive "split by comma" approach.

A valid csv might look like this:

id,author,title
1,Jeremy Grifski,How to Parse a Spreadsheet
2,Juha-Matti Santala,"Quotes, they are great in csv"
Enter fullscreen mode Exit fullscreen mode

Here, the split-by-comma approach will actually trip up because it doesn't regard the quoted string as a field.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Good point! I’ll add that note to the first section.

Collapse
 
brunogirin profile image
Bruno Girin

Good article. Another method to be aware of, if you are using the pandas library to process your data further, that library can read a CSV or Excel file into a DataFrame: pandas.pydata.org/pandas-docs/stab...

Collapse
 
vorsprung profile image
vorsprung

Probably good to mention in passing that reading/writing XLSX files directly works fine with an add on library