Created
October 15, 2022 00:32
-
-
Save turnipsoup/6d984ae077d6cf09e81177559b79ba1e to your computer and use it in GitHub Desktop.
Fix PGNs that are broken by the MacOS Stockfish app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
# Get first argument | |
filename = sys.argv[1] | |
# Open the file | |
pgn = open(filename, "r").read().strip() | |
# Split it into headers and moves | |
pgns = pgn.split("\n\n") | |
clean_pgn = [] | |
pgn_target = [] | |
# If there are no headers, just one the moves | |
if len(pgns) == 1: | |
pgn_target = pgns[0] | |
else: | |
pgn_target = pgns[1] | |
for move in pgn_target.split(" "): | |
try: | |
val = int(move[0]) | |
clean_pgn.append(move) | |
except: | |
clean_pgn.append(" {" + move + "} ") | |
clean_gn_str = "".join(clean_pgn).replace("\n", " ") | |
if len(pgns) == 1: | |
#print(clean_gn_str) | |
with open(filename, "w") as f: | |
f.write(clean_gn_str) | |
else: | |
with open(filename, "w") as f: | |
f.write("\n\n".join([pgns[0], clean_gn_str])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
python3 fixpgn.py mypgn.pgn