Sort a PGN file by dates?

Discussion of chess software programming and technical issues.

Moderator: Ras

kinderchocolate
Posts: 454
Joined: Mon Nov 01, 2010 6:55 am
Full name: Ted Wong

Sort a PGN file by dates?

Post by kinderchocolate »

Anybody has a script that can sort games in a chess PGN file by dates?
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Sort a PGN file by dates?

Post by bob »

kinderchocolate wrote:Anybody has a script that can sort games in a chess PGN file by dates?
A c-shell script would be pretty easy, but the obvious question is, "does your pgn have a consistent date format?" None I have used do, which would complicate this significantly.
kinderchocolate
Posts: 454
Joined: Mon Nov 01, 2010 6:55 am
Full name: Ted Wong

Re: Sort a PGN file by dates?

Post by kinderchocolate »

My files have very consistent date format. I could make my own script but I thought somebody might have already done it. I guess I'll have to program it myself.
mvk
Posts: 589
Joined: Tue Jun 04, 2013 10:15 pm

Re: Sort a PGN file by dates?

Post by mvk »

kinderchocolate wrote:Anybody has a script that can sort games in a chess PGN file by dates?
Here is one in Python:

Code: Select all

import sys
import re

class Game:
        def __init__(self):
                self.lines = []
                self.date = ''
                self.time = ''
        def append(self, line):
                self.lines.append(line)
                if re.match('^\[Date ', line):
                        self.date = line
                if re.match('^\[Time ', line):
                        self.time = line
        def emit(self):
                for line in self.lines:
                        print line,

def readfile(filename, games):
        fp = open(filename, 'r')
        current = None
        for line in fp:
                if re.match('^\[Event ', line):
                        if current:
                                games.append(current)
                        current = Game()
                current.append(line)
        if current: # Don't forget the last one (if any)
                games.append(current)
        fp.close()

games = []
for arg in sys.argv[1:]:
        readfile(arg, games)
games.sort(key=lambda g: (g.date, g.time))
for game in games:
        game.emit()
Usage: python pgnsort.py file1.pgn file2.pgn ...etc... > sorted.pgn
[Account deleted]
kinderchocolate
Posts: 454
Joined: Mon Nov 01, 2010 6:55 am
Full name: Ted Wong

Re: Sort a PGN file by dates?

Post by kinderchocolate »

Thanks. Brilliant, it works. I changed the line to

games.sort(key=lambda g: (g.date, g.time),reverse=True)

for sorting from the latest.

It works, thanks.


mvk wrote:
kinderchocolate wrote:Anybody has a script that can sort games in a chess PGN file by dates?
Here is one in Python:

Code: Select all

import sys
import re

class Game:
        def __init__(self):
                self.lines = []
                self.date = ''
                self.time = ''
        def append(self, line):
                self.lines.append(line)
                if re.match('^\[Date ', line):
                        self.date = line
                if re.match('^\[Time ', line):
                        self.time = line
        def emit(self):
                for line in self.lines:
                        print line,

def readfile(filename, games):
        fp = open(filename, 'r')
        current = None
        for line in fp:
                if re.match('^\[Event ', line):
                        if current:
                                games.append(current)
                        current = Game()
                current.append(line)
        if current: # Don't forget the last one (if any)
                games.append(current)
        fp.close()

games = []
for arg in sys.argv[1:]:
        readfile(arg, games)
games.sort(key=lambda g: (g.date, g.time))
for game in games:
        game.emit()
Usage: python pgnsort.py file1.pgn file2.pgn ...etc... > sorted.pgn