1

我在谷歌 colab 笔记本中使用 python 国际象棋模块。具体来说,我正在运行与此模块相关的三个命令:

  • chess.pgn.read_game(pgn_moves).mainline_moves()
  • chess.pgn.read_game(pgn_sides).headers["White"]
  • chess.pgn.read_game(pgn_variant).headers["Variant"]

而 chess.pgn.read_game.headers 就像一个魅力,第一个命令抛出一个 AttributeError: 'Game' object has no attribute 'mainline_moves'。奇怪的是,我的脚本在本地运行时运行良好(我在 conda python 3.8.10 环境中使用 Spyder 5)。

stackoverflow 上出现此类错误的其他问题似乎与名为 chess.py 的用户文件有关,因此产生了命名问题。但是,在我看来,这似乎不太可能发生在谷歌 colab 笔记本中......

有关我在国际象棋环境中解决有关 GAN 的烦人(尚未解决的问题)的更多信息,请查看如何在 tensorflow (keras) 中使用 GPU 和 CPU?并且TF 模型在切换到 GPU 后不再预测

我的代码:

import os
os.getcwd()

'''
The following section creates two lists based on a pgn file. One list includes
all moves made per game in the file, whereas the other lists the players names.
Various lists have to be introduced because of the incremental characteristic
of chess.pgn's read_game function. Additional code was necesary in order to 
eliminate non-standard variants (i.e. chess960 games) from the game list.
'''
import chess.pgn
file = "txt_files/games.txt"
pgn_file = open(file)
list_games = []
sides = []
variant = []
drop_list = []
length = 5000

game_list = []
for i in range(length):
    game_list.append(chess.pgn.read_game(pgn_file))
game_list = list(filter(None, game_list))
    
pgn_moves = open(file)
pgn_sides = open(file)
pgn_variant = open(file)

for i in range(len(game_list)):
    try:
        list_games.append(chess.pgn.read_game(pgn_moves).mainline_moves())
        sides.append(chess.pgn.read_game(pgn_sides).headers["White"])
        variant.append(chess.pgn.read_game(pgn_variant).headers["Variant"])
    except:
        print('Error')
        pass
    if variant[i] != 'Standard':
        #print('here here here', i, game_list[i])
        drop_list.append(i)
4

1 回答 1

0

卸载 python-chess 然后安装新版本的 python chess。

!pip uninstall python-chess
!pip install chess

测试

for moves in list_games:
    print(moves)

输出

1. d4 { [%clk 0:15:00] } 1... d6 { [%clk 0:15:00] } 2. c4 { [%clk 0:14:54] } 2... Nf6 { [%clk 0:14:58] } ...
于 2021-11-29T12:28:41.040 回答