def chess_pgn_graph(pgn_file='chess_masters_WCC.pgn.bz2'):
 import bz2
 G = nx.MultiDiGraph()
 game = {}
 datafile = bz2.BZ2File(pgn_file)
 lines = (line.decode().rstrip('\r\n') for line in datafile)
 for line in lines:
  if line.startswith('['):
   (tag, value) = line[1:(-1)].split(' ', 1)
   game[str(tag)] = value.strip('"')
  elif game:
   white = game.pop('White')
   black = game.pop('Black')
   G.add_edge(white, black, **game)
   game = {}
 return G

def chess_pgn_graph ( pgn_file = 'chess_masters_WCC.pgn.bz2' ) : import bz2 G = nx . MultiDiGraph ( ) game = { } datafile = bz2 . BZ2File ( pgn_file ) lines = ( line . decode ( ) . rstrip ( '\r\n' ) for line in datafile ) for line in lines : if line . startswith ( '[' ) : ( tag , value ) = line [ 1 : ( - 1 ) ] . split ( ' ' , 1 ) game [ str ( tag ) ] = value . strip ( '"' ) elif game : white = game . pop ( 'White' ) black = game . pop ( 'Black' ) G . add_edge ( white , black , ** game ) game = { } return G
read chess games in pgn format in pgn_file .