pgn2data
This 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.
Installation
The library requires Python 3.10 or later.
To install, type the following command on the python terminal:
pip install pgn2data
Implementation
Here 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!")
Pandas
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())
Game information
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)
Examples
The folder 'samples' in this repository, has some examples of the output from the library.
You can also go here to see a Kaggle project that converted all of Magnus Carlsen's online Bullet games into CSV format.
Columns
This is a full list of the columns in each output file:
Games File
| Field | Description |
|---|---|
| game_id | ID of game generated by process |
| game_order | Order of game in PGN file |
| event | Event |
| site | Site |
| date_played | Date played |
| round | Round |
| white | White player |
| black | Black player |
| result | Result |
| white_elo | White player rating |
| white_rating_diff | White rating difference from Black |
| black_elo | Black player rating |
| black_rating_diff | Black rating difference from White |
| white_title | Player title |
| black_title | Player title |
| winner | Player name |
| winner_elo | Player rating |
| loser | Losing player |
| loser_elo | Player rating |
| winner_loser_elo_diff | Diff in rating |
| eco | Opening |
| termination | How game ended |
| time_control | Time control |
| utc_date | Date played |
| utc_time | Time played |
| variant | Game type |
| ply_count | Ply Count |
| date_created | Extract date |
| file_name | PGN source file |
Moves File
| Field | Description |
|---|---|
| game_id | ID of game that maps to games file |
| move_no | Order of moves |
| move_no_pair | Chess move number |
| player | Player name |
| notation | Standard notation of move |
| move | Before and after piece location |
| from_square | Piece location before |
| to_square | Piece location after |
| piece | Initial of piece name |
| color | Piece color |
| clock_seconds | Remaining time after the move, in seconds; blank when unavailable. |
| fen | Fen position |
| is_check | Is check on board |
| is_check_mate | Is checkmate on board |
| is_fifty_moves | Is 50 move complete |
| is_fivefold_repetition | Is 5 fold repetition on board |
| is_game_over | Is game over |
| is_insufficient_material | Is game over from lack of mating material |
| white_count | Count of white pieces |
| black_count | Count of black pieces |
| white_{piece}_count | Count of white specified piece |
| black_{piece}_count | Count of black specified piece |
| captured_score_for_white | Total of black pieces captured |
| captured_score_for_black | Total of white pieces captured |
| fen_row{number}_{colour)_count | Number of pieces for the specified colour on this row of the board |
| fen_row{number}_{colour}_value | Total value of pieces for the specified colour on this row of the board |
| move_sequence | Sequence of moves up to current position |
Contributions and testing
Contributions are welcome. Bug fixes and new functionality should include appropriate tests demonstrating the expected behaviour.
From the repository root, run:
from testing.tests import run_all_tests
run_all_tests()
The tests use PGN fixtures in testing/pgn and write generated files to
testing/exports. Existing files in testing/exports are deleted before
the tests run.
Add new checks to the relevant test class and ensure they are called by
the test runner. Regression fixtures for GitHub issues belong in
testing/pgn/issues, using filenames such as reported_issue_02.pgn.
Acknowledgements
This project makes use of the python-chess library.
Metadata
Release files for pgn2data 0.0.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pgn2data-0.0.11.tar.gz | 35.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pgn2data-0.0.11-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 70.7 kB
Release files / pgn2data-0.0.11.tar.gz
| Download URL | pgn2data-0.0.11.tar.gz |
|---|---|
| Size | 35.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b16c1fd2ca41c8d84e960cf38fe0e459c2d84bba18e3608acf4cff1c7379670
|
|
BLAKE2b-256 checksum How to use checksums |
885f90432424352a2d69ddd1dc36ba14891a39bf56129c839eb41ed06d40affe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.4
|
Release files / pgn2data-0.0.11-py3-none-any.whl
| Download URL | pgn2data-0.0.11-py3-none-any.whl |
|---|---|
| Size | 35.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8cab75340e58a24080c21c7b6ff2ff403d609974de74609e488c66e5b36f45fb
|
|
BLAKE2b-256 checksum How to use checksums |
6290bec369c80395c22e82943bebc5d76c3116564fed322a9c16d6a8538a569a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.4
|
Security for pgn2data 0.0.11
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.