Package: pgn2data
pgn2dataThis library converts chess pgn files into CSV tabulated data sets.A pgn file can contain one or multiple chess games. The library parses the pgn file and creates two csv files:Games file: contains high level information (e.g. date, site, event, score, players etc...)Moves file: contains the moves for each game  (e.g. notation, squares, fen position, is in check etc...)The two files can be mapped together using a GUID which the process inserts into both files.InstallationThe library requires Python 3.7 or later.To install, type the following command on the python terminal:pip install pgn2dataImplementationHere is a basic example of how to convert a PGN file:from converter.pgn_data import PGNData

pgn_data = PGNData("tal_bronstein_1982.pgn")
pgn_data.export()The following is an example of grouping multiple files into the same output file ("output.csv").pgn_data = PGNData(["file1.pgn","file2.pgn"],"output")
pgn_data.export()The export function has a return object which allows you to quickly check the size and location of the files created:pgn_data = PGNData("tal_bronstein_1982.pgn")
result = pgn_data.export()
result.print_summary()If you want to check if the files have been created before doing further processing you can do the following:pgn_data = PGNData("tal_bronstein_1982.pgn")
result = pgn_data.export()
if result.is_complete:
    print("Files created!")
else:
    print("Files not created!")The result object also provides methods to import the created files into pandas dataframes:pgn_data = PGNData("tal_bronstein_1982.pgn")
result = pgn_data.export()
if result.is_complete:
    
    # read the games file
    games_df = result.get_games_df()
    print(games_df.head())
    
    # read the moves file
    moves_df = result.get_moves_df()
    print(moves_df.head())
    
    # read both files joined together
    combined_df = result.get_combined_df()
    print(combined_df.head())To output the game information only, you can do the following:from converter.pgn_data import PGNData

pgn_data = PGNData("tal_bronstein_1982.pgn")
pgn_data.export(moves_required=False)ExamplesThe folder 'samples' in this repository, has some examples of the output from the library.You can also gohereto see a Kaggle project that converted all of Magnus Carlsen's online Bullet games
into CSV format.ColumnsThis is a full list of the columns in each output file:Games FileFieldDescriptiongame_idID of game generated by processgame_orderOrder of game in PGN fileeventEventsiteSitedate_playedDate playedroundRoundwhiteWhite playerblackBlack playerresultResultwhite_eloWhite player ratingwhite_rating_diffWhite rating difference from Blackblack_eloBlack player ratingblack_rating_diffBlack rating difference from Whitewhite_titlePlayer titleblack_titlePlayer titlewinnerPlayer namewinner_eloPlayer ratingloserLosing playerloser_eloPlayer ratingwinner_loser_elo_diffDiff in ratingecoOpeningterminationHow game endedtime_controlTime controlutc_dateDate playedutc_timeTime playedvariantGame typeply_countPly Countdate_createdExtract datefile_namePGN source fileMoves FileFieldDescriptiongame_idID of game that maps to games filemove_noOrder of movesmove_no_pairChess move numberplayerPlayer namenotationStandard notation of movemoveBefore and after piece locationfrom_squarePiece location beforeto_squarePiece location afterpieceInitial of piece namecolorPiece colorfenFen positionis_checkIs check on boardis_check_mateIs checkmate on boardis_fifty_movesIs 50 move completeis_fivefold_repetitionIs 5 fold repetition on boardis_game_overIs game overis_insufficient_materialIs game over from lack of mating materialwhite_countCount of white piecesblack_countCount of black pieceswhite_{piece}_countCount of white specified pieceblack_{piece}_countCount of black specified piececaptured_score_for_whiteTotal of black pieces capturedcaptured_score_for_blackTotal of white pieces capturedfen_row{number}_{colour)_countNumber of pieces for the specified colour on this row of the boardfen_row{number}_{colour}_valueTotal value of pieces for the specified colour on this row of the boardmove_sequenceSequence of moves up to current positionContributionsContributions are welcome, all modifications should come with appropriate tests demonstrating
an issue has been resolved, or new functionality is working as intended. Pull Requests without tests
will not be merged.The library can be tested by doing the following:from testing.tests import run_all_tests
run_all_tests()New tests should be added to the above method.AcknowledgementsThis project makes use of thepython-chesslibrary.