Answers.Body: <p>Is there any particular reason why it has to be a Python script? If you just want to process the games and extract those matching your criteria then you could use my <a href="https://www.cs.kent.ac.uk/%7Edjb/pgn-extract/" rel="nofollow noreferrer">https://www.cs.kent.ac.uk/~djb/pgn-extract/</a> command-line tool to do that. You can pipe the output of the decompression program as input to pgn-extract and give pgn-extract a file of the tags you are interesting in matching. It will then output matched games, in PGN format, to a file of your choosing.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-22T04:09:32.483Z
Answers.Text: Right, I have just realized that you can use pgn-extract to do that.. If possible, could you please help me get pgn-extract running on MacOS? I only see Windows binaries on the website, and I have downloaded the .tgz file, yet I don't know how to run it.. Thanks
Answers.UserDisplayName: Aryan Pahwa
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-22T06:16:51.833Z
Answers.Text: If you uncompress the .tgz file you will get a folder called pgn-extract. Open a Terminal window and cd into the pgn-extract folder (or open the Terminal in that folder). Then type make and the program should build as pgn-extract in that folder. To run it, type ./pgn-extract in that folder. The documentation describes the options to use for the filtering you want.
Answers.UserDisplayName: kentdjb
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-21T19:34:57.070Z
Answers.LastActivityDate: 2024-07-21T19:34:57.070Z
Answers.UserDisplayName: kentdjb
Body: <p>I would like to create a python script to extract games from a PGN file that is compressed using .pgn.zst, like the lichess.org database.</p>
  <p>For example, one of the files is around 32GB compressed, and around 7 times more than that when uncompressed, so SCID won't work.</p>
  <p>I need to use a program like zstdcat to directly work on huge compressed PGN files and run a python script on them. I need to make one that can search for tags like &quot;WhiteElo&quot; and &quot;BlackElo&quot; and make sure they match a specific value, and extract all games that match them to a file. I also want to select the time controls.</p>
  <p>I need to create a python script that does the following:</p>
  <ul>
  <li>Goes through the PGN file</li>
  <li>Takes every game that have WhiteElo as 2500+, BlackElo as 2500+, and TimeControl as either &quot;180+0&quot;, &quot;180+2&quot;, &quot;300+0&quot;, &quot;300+3&quot;, &quot;600+0&quot;, &quot;600+5&quot;, &quot;900+10&quot;, &quot;300+0&quot;, or &quot;30+20&quot;.</li>
  <li>Extracts those games into another PGN file..</li>
  </ul>
  
Tags: <programming><database><pgn><python>
Title: Python program for extracting games from PGN file based on tags (MacOS)
ThreadText: # Python program for extracting games from PGN file based on tags (MacOS)
  
  (Asked by: username_0 on 2024-07-21)
  
  I would like to create a python script to extract games from a PGN file that is compressed using .pgn.zst, like the lichess.org database.
  
  For example, one of the files is around 32GB compressed, and around 7 times more than that when uncompressed, so SCID won't work.
  
  I need to use a program like zstdcat to directly work on huge compressed PGN files and run a python script on them. I need to make one that can search for tags like "WhiteElo" and "BlackElo" and make sure they match a specific value, and extract all games that match them to a file. I also want to select the time controls.
  
  I need to create a python script that does the following:
  
  * Goes through the PGN file
  * Takes every game that have WhiteElo as 2500+, BlackElo as 2500+, and TimeControl as either "180+0", "180+2", "300+0", "300+3", "600+0", "600+5", "900+10", "300+0", or "30+20".
  * Extracts those games into another PGN file..
  
  **Tags:** programming, database, pgn, python
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-07-21. Score: 1 | Accepted answer
  
  Is there any particular reason why it has to be a Python script? If you just want to process the games and extract those matching your criteria then you could use my [https://www.cs.kent.ac.uk/~djb/pgn-extract/](https://www.cs.kent.ac.uk/%7Edjb/pgn-extract/) command-line tool to do that. You can pipe the output of the decompression program as input to pgn-extract and give pgn-extract a file of the tags you are interesting in matching. It will then output matched games, in PGN format, to a file of your choosing.
  
  > **Comments:**
  > **username_0**: Right, I have just realized that you can use pgn-extract to do that.. If possible, could you please help me get pgn-extract running on MacOS? I only see Windows binaries on the website, and I have downloaded the .tgz file, yet I don't know how to run it.. Thanks  
  > **username_1**: If you uncompress the .tgz file you will get a folder called pgn-extract. Open a Terminal window and cd into the pgn-extract folder (or open the Terminal in that folder). Then type make and the program should build as pgn-extract in that folder. To run it, type ./pgn-extract in that folder. The documentation describes the options to use for the filtering you want.  
  
  ---
  
  