Ramblings

July 6, 2008

Python Excel file reader

Filed under: cool, python, tip, xls — michaelangela @ 1:36 am

This thing is just brilliant! I am working on a project that uses PHP. But I still do my data mangling with Python because of tools like SqlSoup running under SqlAlchemy. Using those under iPython has saved incredible amounts of time. I actually like working with data in this way. It just makes it more fun. Well, I have some data in an Excel file that has multiline text in it. That can be a pain with CSV files. A little digging located a Python Excel writer which had stopped development, with the suggest to use a Python port of a Perl writer, which while cool for writing, has trouble reading… but a helpful post pointed to xlrd. A quick “sudo easy_install-2.5 xlrd” later and I was able to do:

import xlrd
book = xlrd.open_workbook("myfile.xls")
sh = book.sheet_by_index(0)
for r in range(sh.nrows)[1:]:
print sh.row(r)[:4]

Sweet. Indeed. All of the Unicode elements and multilines are intact and good to go. Good stuff.

Python Package Index : xlrd 0.6.1

Library for developers to extract data from Microsoft Excel ™ spreadsheet files

Extract data from new and old Excel spreadsheets on any platform. Pure Python (2.1 or later). Strong support for Excel dates. Unicode-aware.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: