Need help with eco2pgn.py

Discussion of anything and everything relating to chess playing software and machines.

Moderator: Ras

Adam Hair
Posts: 3226
Joined: Wed May 06, 2009 10:31 pm
Location: Fuquay-Varina, North Carolina

Need help with eco2pgn.py

Post by Adam Hair »

I want to convert the scid.eco file that comes with Scid into a pgn file.
The following Python script, eco2pgn, was made to do this. My problem
is that it is writing to standard output and not to a file. Either that or I
do not know where to look for the file. Could somebody tell me how
to modify the script so that it writes to a file, say eco.pgn .

Code: Select all

import re
import sys

if len(sys.argv) < 2:
    sys.stderr.write("usage: eco2pgn file.eco > file.pgn\n")
    sys.stderr.write("eco2pgn takes a Scid openings classification file,\n")
    sys.stderr.write("and writes it in PGN format to standard output.\n")
    sys.exit(1)

fd = open(sys.argv[1], "r")

while 1:
    line = fd.readline()
    if not line: break
    line = line[:-1]

    if re.match("\s*(#|$)", line): continue

    match = re.match("(\S+)\s+\"([^\"]+)\"(\s+.+)?$", line)
    if match:
        eco, variation, cont = match.groups()

        if not cont:
            cont = ''

        while not cont or cont[-1] != '*':
            cont = cont + ' ' + fd.readline()[:-1]

        if cont[-1] == '*' and cont[-2] != ' ':
            cont = cont[:-1] + ' *'

    print '[ECO "%s"]' % eco
    print '[Variation "%s"]' % variation
    print '[Result "*"]'
    print
    print cont
    print
nevatre
Posts: 16
Joined: Fri Aug 01, 2008 6:09 pm
Location: UK

Re: Need help with eco2pgn.py

Post by nevatre »

Hi Adam

don't modify the script, just redirect the output to a file.
Ron Murawski
Posts: 397
Joined: Sun Oct 29, 2006 4:38 am
Location: Schenectady, NY

Re: Need help with eco2pgn.py

Post by Ron Murawski »

Adam Hair wrote:I want to convert the scid.eco file that comes with Scid into a pgn file.
The following Python script, eco2pgn, was made to do this. My problem
is that it is writing to standard output and not to a file. Either that or I
do not know where to look for the file. Could somebody tell me how
to modify the script so that it writes to a file, say eco.pgn .
Why not redirect standard output?

At a Windows command prompt, this is done like this:

Code: Select all

program -param1 -param2 >>filename.extension
Ron
alpha123
Posts: 660
Joined: Sat Dec 05, 2009 5:13 am
Location: Colorado, USA

Re: Need help with eco2pgn.py

Post by alpha123 »

No need to modify it, but this would write to a specified file name:

Code: Select all

import re
import sys

if len(sys.argv) < 2:
    sys.stderr.write("usage: eco2pgn file.eco file.pgn\n")
    sys.stderr.write("eco2pgn takes a Scid openings classification file,\n")
    sys.stderr.write("and writes it in PGN format to a file.\n")
    sys.exit(1)

fd = open(sys.argv[1], "r")

while 1:
    line = fd.readline()
    if not line: break
    line = line[:-1]

    if re.match("\s*(#|$)", line): continue

    match = re.match("(\S+)\s+\"([^\"]+)\"(\s+.+)?$", line)
    if match:
        eco, variation, cont = match.groups()

        if not cont:
            cont = ''

        while not cont or cont[-1] != '*':
            cont = cont + ' ' + fd.readline()[:-1]

        if cont[-1] == '*' and cont[-2] != ' ':
            cont = cont[:-1] + ' *'

    output = open(sys.argv[2], 'w')

    output.write('[ECO "' + eco + '"]\n')
    output.write('[Variation "' + variation + '"]\n')
    output.write('[Result "*"]\n\n')
    output.write(cont + '\n')
And do: python eco2pgn.py eco.eco eco.pgn

EDIT: Note I haven't checked that it works

Peter
alpha123
Posts: 660
Joined: Sat Dec 05, 2009 5:13 am
Location: Colorado, USA

Re: Need help with eco2pgn.py

Post by alpha123 »

Actually, it should be this:

Code: Select all

import re
import sys

if len(sys.argv) < 2:
    sys.stderr.write("usage: eco2pgn file.eco file.pgn\n")
    sys.stderr.write("eco2pgn takes a Scid openings classification file,\n")
    sys.stderr.write("and writes it in PGN format to a file.\n")
    sys.exit(1)

fd = open(sys.argv[1], "r")

while 1:
    line = fd.readline()
    if not line: break
    line = line[:-1]

    if re.match("\s*(#|$)", line): continue

    match = re.match("(\S+)\s+\"([^\"]+)\"(\s+.+)?$", line)
    if match:
        eco, variation, cont = match.groups()

        if not cont:
            cont = ''

        while not cont or cont[-1] != '*':
            cont = cont + ' ' + fd.readline()[:-1]

        if cont[-1] == '*' and cont[-2] != ' ':
            cont = cont[:-1] + ' *'
    
    output = open(sys.argv[2], 'a')
    
    output.write('[ECO "' + eco + '"]\n')
    output.write('[Variation "' + variation + '"]\n')
    output.write('[Result "*"]\n')
    output.write(cont + '\n\n')
EDIT: Oh, and this one I tested. (With the ChessDB eco file.)

Peter
Adam Hair
Posts: 3226
Joined: Wed May 06, 2009 10:31 pm
Location: Fuquay-Varina, North Carolina

Re: Need help with eco2pgn.py

Post by Adam Hair »

nevatre wrote:Hi Adam

don't modify the script, just redirect the output to a file.
Ron wrote:Why not redirect standard output?

At a Windows command prompt, this is done like this:
Code:
program -param1 -param2 >>filename.extension


Ron
I didn't know about redirecting the output until the two of you mentioned
it. I'll learned something new. :)

Funny enough, I could not do it the way Ron said to do it ( :?: ), but
I made the following batch file and it worked:

Code: Select all

eco2pgn.py scid.eco >>eco.pgn
Thanks Neil and Ron

Adam
Adam Hair
Posts: 3226
Joined: Wed May 06, 2009 10:31 pm
Location: Fuquay-Varina, North Carolina

Re: Need help with eco2pgn.py

Post by Adam Hair »

alpha123 wrote:Actually, it should be this:

Code: Select all

import re
import sys

if len(sys.argv) < 2:
    sys.stderr.write("usage: eco2pgn file.eco file.pgn\n")
    sys.stderr.write("eco2pgn takes a Scid openings classification file,\n")
    sys.stderr.write("and writes it in PGN format to a file.\n")
    sys.exit(1)

fd = open(sys.argv[1], "r")

while 1:
    line = fd.readline()
    if not line: break
    line = line[:-1]

    if re.match("\s*(#|$)", line): continue

    match = re.match("(\S+)\s+"([^"]+)"(\s+.+)?$", line)
    if match:
        eco, variation, cont = match.groups()

        if not cont:
            cont = ''

        while not cont or cont[-1] != '*':
            cont = cont + ' ' + fd.readline()[:-1]

        if cont[-1] == '*' and cont[-2] != ' ':
            cont = cont[:-1] + ' *'
    
    output = open(sys.argv[2], 'a')
    
    output.write('[ECO "' + eco + '"]\n')
    output.write('[Variation "' + variation + '"]\n')
    output.write('[Result "*"]\n')
    output.write(cont + '\n\n')
EDIT: Oh, and this one I tested. (With the ChessDB eco file.)

Peter
Thanks Peter. At the very least it gives me an example to go by if I
start trying to learn Python.
Ron Murawski
Posts: 397
Joined: Sun Oct 29, 2006 4:38 am
Location: Schenectady, NY

Re: Need help with eco2pgn.py

Post by Ron Murawski »

Adam Hair wrote:
Ron wrote:Why not redirect standard output?

At a Windows command prompt, this is done like this:
Code:
program -param1 -param2 >>filename.extension


Ron
Funny enough, I could not do it the way Ron said to do it ( :?: ), but
I made the following batch file and it worked:

Code: Select all

eco2pgn.py scid.eco >>eco.pgn
Thanks Neil and Ron

Adam
I was trying to show you the general case. In your specific case: the 'program' was:
eco2pgn.py
there was only one parameter, '-param1', needed:
scid.eco
and your redirected file was:
eco.pgn

I didn't bother with that stuff because I thought you needed to know about the redirect characters:
>>

It's impossible to guess other people's expertise. You were bright enough to guess what I meant, though! :-)

Ron
alpha123
Posts: 660
Joined: Sat Dec 05, 2009 5:13 am
Location: Colorado, USA

Re: Need help with eco2pgn.py

Post by alpha123 »

Adam Hair wrote:Thanks Peter. At the very least it gives me an example to go by if I start trying to learn Python.
You should definitely learn Python. It's a wonderful language, IMO.

Peter
Adam Hair
Posts: 3226
Joined: Wed May 06, 2009 10:31 pm
Location: Fuquay-Varina, North Carolina

Re: Need help with eco2pgn.py

Post by Adam Hair »

Ron Murawski wrote:
Adam Hair wrote:
Ron wrote:Why not redirect standard output?

At a Windows command prompt, this is done like this:
Code:
program -param1 -param2 >>filename.extension


Ron
Funny enough, I could not do it the way Ron said to do it ( :?: ), but
I made the following batch file and it worked:

Code: Select all

eco2pgn.py scid.eco >>eco.pgn
Thanks Neil and Ron

Adam
I was trying to show you the general case. In your specific case: the 'program' was:
eco2pgn.py
there was only one parameter, '-param1', needed:
scid.eco
and your redirected file was:
eco.pgn

I didn't bother with that stuff because I thought you needed to know about the redirect characters:
>>

It's impossible to guess other people's expertise. You were bright enough to guess what I meant, though! :-)

Ron
I also could not make c:\filepath\eco2pgn.py scid.eco >>eco.pgn work,
by which I mean redirect to eco.pgn. I have had trouble a few other
times in the past running programs from the command line. There must
be something I am not doing right. However, every time I have had
trouble with the command line, I have been able to write a batch file
as a work around.

As far as my expertise goes, it is miniscule. Most of what I have learned
has been from mimicking what I have seen in other people's batch files
and instructions and then figuring out what I did wrong when something
does not run properly. I was really happy when you said to redirect the
output. I sort of knew what was going on, but I lacked the proper term
( redirect ) to find the info I needed on Google.

Thanks again Ron.

Adam