Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Converts the millionbase chess PGN database ( to json
# Converts the millionbase chess PGN database ( to json
# with one json dictionary per row. (That is, the resulting file is contain multiple json objects,
# not just one large).
import json
import chess.pgn # From python-chess
pgn = open("millionbase-2.22.pgn") # Or where you have put it
fout = open('milionbase-2.22.json', 'w') # Or where you want it
count = 0
node = chess.pgn.read_game(pgn)
while node != None:
info = node.headers
info["fen"] = []
while node.variations:
next_node = node.variation(0)
node = next_node
node = chess.pgn.read_game(pgn)
json.dump(info, fout, encoding='latin1')
count += 1
if(count % 10000 == 0):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment