SgftosvScript
If someone could help me with the formatting so it looks proper would be much appreciated (so for example square brackets are displayed). Anyway you can also click edit and copy it from there...
Chris
from os import walk from os.path import join
#First some options here.
RootDir = "d:\\games\\go\\games\\pro" OutputFile = "d:\\games\\go\\games\\protable.csv" Properties = !['pb', 'pw', 'br', 'wr', 'dt', 'ev', 're']
print """ SGF Database Maker ==================
Use this program to create a CSV file with sgf info. """
def getInfo(filename): """Read out file info here and return a dictionary with all the properties needed.""" result = ![]
file = open(filename, 'r') data = file.read(1024) read at most 1kb since we assume all relevant info is in the beginning file.close()
for prop in Properties: try: i = data.lower().index(prop) except ValueError: result.append((prop, '')) continue try: value = data[data.index('[', i)+1 : data.index(']', i)] except ValueError: value = ''
result.append((prop, value))
return dict(result)
ProgressCounter = 0
file = open(OutputFile, "w") file.write('^Filename^;^PB^;^BR^;^PW^;^WR^;^RE^;^EV^;^DT^\n')
for root, dirs, files in walk(RootDir): for name in files: if name[-3:].lower() != "sgf": continue
info = getInfo(join(root, name))
file.write('^'+join(root, name)+'^;^'+info['pb']+'^;^'+info['br']+'^;^'+info['pw']+'^;^'+info['wr']+'^;^'+info['re']+'^;^'+info['ev']+'^;^'+info['dt']+'^\n') ProgressCounter += 1 if (ProgressCounter) % 100 == 0: print str(ProgressCounter) + " games processed."
file.close()
print "A total of " + str(ProgressCounter) + " have been processed."