Need help with eco2pgn.py

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

Moderators: hgm, Rebel, chrisw

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&#40;sys.argv&#41; < 2&#58;
    sys.stderr.write&#40;"usage&#58; eco2pgn file.eco > file.pgn\n")
    sys.stderr.write&#40;"eco2pgn takes a Scid openings classification file,\n")
    sys.stderr.write&#40;"and writes it in PGN format to standard output.\n")
    sys.exit&#40;1&#41;

fd = open&#40;sys.argv&#91;1&#93;, "r")

while 1&#58;
    line = fd.readline&#40;)
    if not line&#58; break
    line = line&#91;&#58;-1&#93;

    if re.match&#40;"\s*(#|$)", line&#41;&#58; continue

    match = re.match&#40;"(\S+)\s+\"(&#91;^\"&#93;+)\"(\s+.+)?$", line&#41;
    if match&#58;
        eco, variation, cont = match.groups&#40;)

        if not cont&#58;
            cont = ''

        while not cont or cont&#91;-1&#93; != '*'&#58;
            cont = cont + ' ' + fd.readline&#40;)&#91;&#58;-1&#93;

        if cont&#91;-1&#93; == '*' and cont&#91;-2&#93; != ' '&#58;
            cont = cont&#91;&#58;-1&#93; + ' *'

    print '&#91;ECO "%s"&#93;' % eco
    print '&#91;Variation "%s"&#93;' % variation
    print '&#91;Result "*"&#93;'
    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&#40;sys.argv&#41; < 2&#58;
    sys.stderr.write&#40;"usage&#58; eco2pgn file.eco file.pgn\n")
    sys.stderr.write&#40;"eco2pgn takes a Scid openings classification file,\n")
    sys.stderr.write&#40;"and writes it in PGN format to a file.\n")
    sys.exit&#40;1&#41;

fd = open&#40;sys.argv&#91;1&#93;, "r")

while 1&#58;
    line = fd.readline&#40;)
    if not line&#58; break
    line = line&#91;&#58;-1&#93;

    if re.match&#40;"\s*(#|$)", line&#41;&#58; continue

    match = re.match&#40;"(\S+)\s+\"(&#91;^\"&#93;+)\"(\s+.+)?$", line&#41;
    if match&#58;
        eco, variation, cont = match.groups&#40;)

        if not cont&#58;
            cont = ''

        while not cont or cont&#91;-1&#93; != '*'&#58;
            cont = cont + ' ' + fd.readline&#40;)&#91;&#58;-1&#93;

        if cont&#91;-1&#93; == '*' and cont&#91;-2&#93; != ' '&#58;
            cont = cont&#91;&#58;-1&#93; + ' *'

    output = open&#40;sys.argv&#91;2&#93;, 'w')

    output.write&#40;'&#91;ECO "' + eco + '"&#93;\n')
    output.write&#40;'&#91;Variation "' + variation + '"&#93;\n')
    output.write&#40;'&#91;Result "*"&#93;\n\n')
    output.write&#40;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&#40;sys.argv&#41; < 2&#58;
    sys.stderr.write&#40;"usage&#58; eco2pgn file.eco file.pgn\n")
    sys.stderr.write&#40;"eco2pgn takes a Scid openings classification file,\n")
    sys.stderr.write&#40;"and writes it in PGN format to a file.\n")
    sys.exit&#40;1&#41;

fd = open&#40;sys.argv&#91;1&#93;, "r")

while 1&#58;
    line = fd.readline&#40;)
    if not line&#58; break
    line = line&#91;&#58;-1&#93;

    if re.match&#40;"\s*(#|$)", line&#41;&#58; continue

    match = re.match&#40;"(\S+)\s+\"(&#91;^\"&#93;+)\"(\s+.+)?$", line&#41;
    if match&#58;
        eco, variation, cont = match.groups&#40;)

        if not cont&#58;
            cont = ''

        while not cont or cont&#91;-1&#93; != '*'&#58;
            cont = cont + ' ' + fd.readline&#40;)&#91;&#58;-1&#93;

        if cont&#91;-1&#93; == '*' and cont&#91;-2&#93; != ' '&#58;
            cont = cont&#91;&#58;-1&#93; + ' *'
    
    output = open&#40;sys.argv&#91;2&#93;, 'a')
    
    output.write&#40;'&#91;ECO "' + eco + '"&#93;\n')
    output.write&#40;'&#91;Variation "' + variation + '"&#93;\n')
    output.write&#40;'&#91;Result "*"&#93;\n')
    output.write&#40;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&#40;sys.argv&#41; < 2&#58;
    sys.stderr.write&#40;"usage&#58; eco2pgn file.eco file.pgn\n")
    sys.stderr.write&#40;"eco2pgn takes a Scid openings classification file,\n")
    sys.stderr.write&#40;"and writes it in PGN format to a file.\n")
    sys.exit&#40;1&#41;

fd = open&#40;sys.argv&#91;1&#93;, "r")

while 1&#58;
    line = fd.readline&#40;)
    if not line&#58; break
    line = line&#91;&#58;-1&#93;

    if re.match&#40;"\s*(#|$)", line&#41;&#58; continue

    match = re.match&#40;"(\S+)\s+"(&#91;^"&#93;+)"(\s+.+)?$", line&#41;
    if match&#58;
        eco, variation, cont = match.groups&#40;)

        if not cont&#58;
            cont = ''

        while not cont or cont&#91;-1&#93; != '*'&#58;
            cont = cont + ' ' + fd.readline&#40;)&#91;&#58;-1&#93;

        if cont&#91;-1&#93; == '*' and cont&#91;-2&#93; != ' '&#58;
            cont = cont&#91;&#58;-1&#93; + ' *'
    
    output = open&#40;sys.argv&#91;2&#93;, 'a')
    
    output.write&#40;'&#91;ECO "' + eco + '"&#93;\n')
    output.write&#40;'&#91;Variation "' + variation + '"&#93;\n')
    output.write&#40;'&#91;Result "*"&#93;\n')
    output.write&#40;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