1

Helo!

Using chess.pgn to convert a Chess database into a dataframe, to read the nth game from the database do I need to read all the previous ones first? I can't jump directly to the game n? If I want to distribute the processing in a database with 10^8 games, I can't start reading in the 9e7th game?

import pandas as pd
import chess.pgn
from datetime import datetime as dt
import os
import glob

nome_arquivo = "Analises_01.pgn"
inicio = 0
numero_jogos = 1.47e8

arquivo = open(nome_arquivo, encoding="utf8")

ratings = []
for j in range(numero_jogos):
    first_game = chess.pgn.read_game(arquivo)
    if j >= inicio:
        try:
            Brancas = int(first_game.headers["WhiteElo"])
            Pretas = int(first_game.headers["BlackElo"])
            ratings.append([Brancas, Pretas])
        except:
            pass
Hick Nax
  • 43
  • 1
  • 5

3 Answers3

1

I hope this code can help you. I didn't use Pandas or a data frame, sorry. It will just make a list to indexing all the pgn games. So, game_index[n] will return the string of the game number n+1.

PGN = open('your_pgn_path_here.pgn')
text_PGN = PGN.read()
game_index = []
actual_game = ''
for string in text_PGN :
    if string == '\n' :
        if actual_game[-2] == '\n' and actual_game[-1] == '\n' :
            actual_game += string
            game_index.append(actual_game)
            actual_game = ''
        else :
            actual_game += string
    else :
        actual_game += string
LittleCoder
  • 391
  • 1
  • 13
0
import chess.pgn
import pandas as pd

pgn = open("your_pgn_path_here.pgn")

my_list = []
for i in pgn:
    i = chess.pgn.read_game(pgn)
    my_list.append(i)
    df = pd.DataFrame(my_list)

#shows 210 game in dataframe    
print(df[0][210])
0

You could turn the PGN file into a CSV file first with tools such as pgn2data, then read the CSV into pandas:

from converter.pgn_data import PGNData
import pandas as pd

# Convert PGN to CSV
pgn_data = PGNData("Analises_01.pgn")
pgn_data.export() 

# Load the two files created by export() to pandas
game_info_df = pd.read_csv("Analises_01_game_info.csv")
game_moves_df = pd.read_csv("Analises_01_moves.csv")

The Analises_01_moves.csv contains a row for each move, so you will probably want to perform some game_moves_df.groupby operation to join the dataframes into one.

Filyb
  • 1