Newbie question about file input

Aaron Deskins ndeskins at ecn.purdue.edu
Tue Aug 17 09:50:31 EDT 2004


Ok everyone,
    Thanks again for all the input. I rewrote the code and have included 
some more routines. Basically the program takes a pgn file with all the 
chess games and randomizes them and spits out a new pgn file. It is 
below. Hopefully I was able to get the spacing right. :)

    Also, what exactly is stored when a blank line is read by the 
readline command? A zero, blank, or what??? What is the difference 
between trying to read a non-existant line (i.e. a line at the end of 
the file) and a blank line (line with nothing on it)?

  Thanks again.



pgn-rand.py:

#! /usr/bin/env python
import string
import sys
import random

random.seed

# Find number of games, it
zf=open(sys.argv[1],'r')
it = 0
while 1:
     line = zf.readline()
     if line.startswith("[Event"):
         it+=1
     if not line:
         break
zf.close()

# Initialize order which has list of games
a = 0
order = [1]
while a < it-1:
     order.append(a+2)
     a+=1

# Randomize game list 4x
a=0
while a < 4*it:
     one = random.randint(0,it-1)
     two = random.randint(0,it-1)
     temp = order[one]
     order[one]=order[two]
     order[two]=temp
     a+= 1

# Output to new pgn file in new order
out=open(sys.argv[2],'w')
a=0
while a < it:
     curr=0
     zf=open(sys.argv[1],'r')
     while 1:
         line = zf.readline()
         if line.startswith("[Event"):
             curr+= 1
         if order[a] == curr:
             out.write(line)
         if not line:
             break
     zf.close()
     a+=1
out.close()

-- 
Aaron Deskins
Graduate Student
Chemical Engineering
Purdue University



More information about the Python-list mailing list