[Python] How to extract scores in a PGN file using python chess.pgn.read_game function

Discussão em 'Python' iniciado por Stack, Dezembro 8, 2024.

  1. Stack

    Stack Membro Participativo

    PGN with scores like this:

    1. e4 {+0.42/27 1.6s} e5 {-0.21/26 5.4s}
    2. Nf3 {+0.34/22 0.94s} Nc6 {-0.16/19 0.37s}
    3. Bb5 {+0.35/27 2.1s} Nf6 {-0.19/26 0.80s}
    4. O-O {+0.33/22 0.56s} Nxe4 {-0.16/26 0.92s}
    5. Re1 {+0.29/27 1.0s} Nd6 {-0.19/24 0.66s}
    6. Nxe5 {+0.32/24 0.59s, No result} *


    my code is as follows:

    self.move_stack = []

    with open(file_path) as f:
    game = chess.pgn.read_game(f)
    if game:
    self.board = game.board()
    self.move_stack = list(game.mainline_moves())


    It can extract moves, but I don't know how to extract the scores like 0,42.

    I have read the documents about how to parse PGN file. It says eval() should work. But I tried with game.eval() and game.variations[0].eval(), both of them didn't work.

    game.variations[0].comment can get the whole comment (+0.42/27 1.6s). I just want the first part 0.42.

    finally, I managed to use a stupid way to get the scores.

    i=0
    for x in move_comment:
    if x!= "/":
    i=i+1
    else:
    break
    original_score=move_comment[0:i]
    The_original_scores.append(float(original_score))


    There must be a simple way to do it.

    Continue reading...

Compartilhe esta Página