Speaking of the many libraries people have written I thought I'd mention pychess. Don't remember where I found it, but it's easy enough to find on a search engine. That's the whole reason I discovered python, searching for a good algorithm to use in parsing chess pgn files.