def make_dfs(filename, playername): pgn_text = open(filename).read() pgn_game = pgn.PGNGame() dat = pgn.loads(pgn_text) # Returns a list of PGNGame sdat = pgn.dumps(pgn_game) # Returns a string with a pgn game f_g = dat[0] games = list() for game in dat: keys = [key for key in dir(game) if not '_' in key] game_dict = dict() for key in keys: game_dict[key] = game.__getattribute__(key) games.append(game_dict) df = pd.DataFrame(games) split_timecontrol(df) df.whiteelo = pd.to_numeric(df.whiteelo, errors='coerce') df.blackelo = pd.to_numeric(df.blackelo, errors='coerce') #df.dropna(inplace=True) me_black = df.loc[df.black == playername] me_white = df.loc[df.white == playername] return me_black, me_white, df
def test_dumps_special(self): '''Tests ``dumps`` function with move commentary and null tag''' game = pgn.PGNGame('XYZ') game.moves = ['{comment}', 'e4', 'e5', '{in}', 'd4', '{lol}', '1-0'] dump = pgn.dumps(game) first_expected = '[Event "XYZ"]\n[Site "?"]' last_expected = '{comment} 1. e4 e5 {in} 2. d4 {lol} 1-0' assert dump.startswith(first_expected) assert dump.endswith(last_expected)
import pgn game = pgn.PGNGame( 'Rosenwald Memorial', 'Game of the Century', '1956.10.17', '8', 'Donald Byrne', 'Robert James Fischer', '0-1' ) game.plycount = '82' game.moves = ['Nf3', 'Nf6', 'c4', 'g6', 'Nc3', 'Bg7', 'd4', 'O-O', 'Bf4', 'd5', 'Qb3', 'dxc4', 'Qxc4', 'c6', 'e4', 'Nbd7', 'Rd1', 'Nb6', 'Qc5', 'Bg4', 'Bg5', 'Na4', 'Qa3', 'Nxc3', 'bxc3', 'Nxe4', 'Bxe7', 'Qb6', 'Bc4', 'Nxc3', 'Bc5', 'Rfe8+', 'Kf1', 'Be6', 'Bxb6', 'Bxc4+', 'Kg1', 'Ne2+', 'Kf1', 'Nxd4+', 'Kg1', 'Ne2+', 'Kf1', 'Nc3+', 'Kg1', 'axb6', 'Qb4', 'Ra4', 'Qxb6', 'Nxd1', 'h3', 'Rxa2', 'Kh2', 'Nxf2', 'Re1', 'Rxe1', 'Qd8+', 'Bf8', 'Nxe1', 'Bd5', 'Nf3', 'Ne4', 'Qb8', 'b5', 'h4', 'h5', 'Ne5', 'Kg7', 'Kg1', 'Bc5+', 'Kf1', 'Ng3+', 'Ke1', 'Bb4+', 'Kd1', 'Bb3+', 'Kc1', 'Ne2+', 'Kb1', 'Nc3+', 'Kc1', 'Rc2', '0-1'] print(pgn.dumps(game))
def test_dumps_multi(self): '''Tests ``dumps`` function for a list of games''' games = [game_fixture(), game_fixture()] dump = pgn.dumps(games) assert dump == PGN_TEXT+'\n\n\n'+PGN_TEXT
def test_dumps_single(self): '''Tests ``dumps`` function for a single game''' game = game_fixture() dump = pgn.dumps(game) assert dump == PGN_TEXT
# @File : pgnparser1.py # @Software: PyCharm """ pgnparser1.py: """ import pgn # pgn_text = open('morphy.pgn').read() pgn_text = open('example.pgn').read() pgn_game = pgn.PGNGame() x = pgn.loads(pgn_text) print(x) # Returns a list of PGNGame # [<PGNGame "Fischer, Robert J." vs "Spassky, Boris V.">] d = pgn.dumps(pgn_game) print(d) # Returns a string with a pgn game ''' [<PGNGame "Fischer, Robert J." vs "Spassky, Boris V.">] [Event "?"] [Site "?"] [Date "?"] [Round "?"] [White "?"] [Black "?"] [Result "?"] ''' #有问题 ,iter() returned non-iterator of type 'GameStringIterator' # for game in pgn.GameIterator('example.pgn'): # print(game)
def open_file(fi): pgn_text = open(fi).read() pgn_game = pgn.PGNGame(pgn_text) #print pgn.loads(pgn_text) print pgn.dumps(pgn_game)