1

嗨!

使用 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
4

2 回答 2

1

我希望这段代码可以帮助你。抱歉,我没有使用 Pandas 或数据框。它只会列出索引所有 pgn 游戏的列表。因此,game_index[n]将返回游戏编号 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
于 2021-08-04T10:17:09.020 回答
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])
于 2022-02-17T01:23:59.470 回答