Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting move information from a pgn file on Python

Tags:

python

chess

How do I go about extracting move information from a pgn file on Python? I'm new to programming and any help would be appreciated.

like image 516
A.J. Avatar asked Jun 28 '11 19:06

A.J.


2 Answers

@Dennis Golomazov I like what Denis did above. To add on to what he did, if you want to extract move information from more than 1 game in a png file, say like in games in chess database png file, use chess.pgn.

import chess.pgn 
png_folder = open('sample.pgn')
current_game = chess.pgn.read_game(png_folder)
png_text = str(current_game.mainline_moves())

read_game() method acts as an iterator so calling it again will grab the next game in the pgn.

like image 158
Boze Avatar answered Sep 28 '22 08:09

Boze


Try pgnparser.

Example code:

import pgn
import sys

f = open(sys.argv[1])
pgn_text = f.read()
f.close()
games = pgn.loads(pgn_text)
for game in games:
    print game.moves
like image 42
Dennis Golomazov Avatar answered Sep 28 '22 09:09

Dennis Golomazov