Wednesday, June 29, 2011

Python CSV module

Oof, Python works in mysterious and curiously documented ways. No, that's not fair - I'm just not down with all the lingo.

I wanted to quick and test to see if I can pull in some CSV data.

test = csv.reader(open ("Support Worklist.csv"))

test is now a type of storage variable called an "iterator". An iterator is an object that uhm.. iterates? It's a bizarre concept because it has basically one method called next() that delivers the next line of the data and once it reaches the end of the data it stops. There is no "previous" or "rewind" method which is really baffling. I have no idea what use it is! Maybe I'm just not thinking very Pythony yet. I need to get through those real popular intro documents "Learning Python the Hard Way" and "Diving Into Python".

So all this quick test did was reveal that I'm pretty ignorant of Python's data types and it's preventing me from doing anything interesting.

Dang. I was really hoping the CSV module actually DID things with CSV files other than read/write them.

EDIT:
Hmm... http://docs.python.org/library/csv.html#csv.DictReader

EDIT x2:
StackOverflow's definition of the "iterator" tag is pretty revealing:
An iterator is an object-oriented programming pattern which for the most part functions similarly to a pointer to an object inside a collection with the added ability to traverse through the collection in a defined manner, agnostic of the actual implementation or even object addresses in physical memory. Iterators may be further limited in particular traversal directions.

No comments:

Post a Comment