将 pgn 数据库转换为 pandas 数据框

convert pgn database to pandas dataframe

喂!

使用chess.pgn将国际象棋数据库转换为数据帧,要从数据库中读取第n个游戏,我需要先读取所有之前的游戏吗?不能直接跳到游戏n?如果我想在一个有10^8场比赛的数据库中分发处理,我无法在第9e7场比赛开始阅读?

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

希望这段代码能帮到你。抱歉,我没有使用 Pandas 或数据框。它只会列出一个索引所有 pgn 游戏的列表。所以,game_index[n]将return游戏编号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
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])