Skip to content

Instantly share code, notes, and snippets.

@turnipsoup
Created October 15, 2022 00:32
Show Gist options
  • Save turnipsoup/6d984ae077d6cf09e81177559b79ba1e to your computer and use it in GitHub Desktop.
Save turnipsoup/6d984ae077d6cf09e81177559b79ba1e to your computer and use it in GitHub Desktop.
Fix PGNs that are broken by the MacOS Stockfish app
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]))
@turnipsoup
Copy link
Author

Usage: python3 fixpgn.py mypgn.pgn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment