file io (lagged values) newbie question

Jussi Salmela tiedon_jano at hotmail.com
Tue Feb 20 08:31:21 EST 2007


hiro kirjoitti:
> Hey there, I'm currently doing data preprocessing (generating lagged
> values for a time series) and I'm having some difficulties trying to
> write a file to disk.  A friend of mine, wrote this quick example for
> me:
>
<snip>
> 
> tweaked code:
> -------------------------------------------------------------------------------------------------------------------
> f=open('c:/kaka.txt','r')
> array=f.readlines()
> f.close()
> f=open('c:/kakaDump.txt','w')
> lineSize = 4
> skip = 4
> condition = 1
> startIndex = 0
> 
> for letter in array:
>        line = []
>        startIndex = array.index(letter)
> 
>        for indexNum in range(startIndex, startIndex + (skip - 1), 1):
>                if indexNum > (len(array) - 1):
>                        break
>                else:
>                        line.append(array[indexNum])
> 
>        for indexNum in range(startIndex + skip, (startIndex +
> lineSize) + 1, 1):
>                if indexNum > (len(array) - 1):
>                        break
>                else:
>                        line.append(array[indexNum])
> 
>        f.writelines(line)
> 
> -------------------------------------------------------------------------------------------------------------------------------
> C:\>more kakaDump.txt
> 1
> 2
> 3
> 5
> 2
> 3
> 4
> 6
> 3
> 4
> 5
> 74
> 5
> 6
> 5
> 6
> 76
> 77
> 
> For those familiar with neural networks, the input file is a time
> series and the output file needs to have 3 lagged variables for
> training and a (two time steps ahead) variable for the target.  Ie:
> 
> input file
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 
> output file
> 1 2 3 5
> 2 3 4 6
> 3 4 5 7
> 4 5 6
> 5 6 7
> 6 7
> 7
> 
> Thanks in advanced,
> 
> 
> D.
> 

I think your file kaka.txt lacks a CR-LF i.e. '\n' i.e. "Enter" after 
the last line.

To get the desired output format you also need to drop the CR-LF:s after 
each line to have the required values printed on the same line. Here's 
my version of your code with a couple remarks added:

#---------------------------------------------------------
f = open('kaka.txt','r')
# The Windows root directory C:\ is a special directory
# designed to be used by Windows itself. To put it
# politely: it's unwise to do program development in
# that directory
array = f.readlines()
f.close()
# This drops the '\n' from each line:
array = [x[:-1] for x in array]
#print array
f = open('kakaDump.txt','w')
lineSize = 4
skip = 4
condition = 1
startIndex = 0

for letter in array:
        line = []
        startIndex = array.index(letter)

        for indexNum in range(startIndex, startIndex + (skip - 1), 1):
                if indexNum > (len(array) - 1):
                        break
                else:
                        line.append(array[indexNum])
# This adds a space between each item in a row
# and after the last item, but it's "only" a space:
                        line.append(' ')

        for indexNum in range(startIndex + skip, (startIndex +
lineSize) + 1, 1):
                if indexNum > (len(array) - 1):
                        break
                else:
                        line.append(array[indexNum])
# This completes the line:
        line.append('\n')

        f.writelines(line)
f.close()
#---------------------------------------------------------

I also have my own completely different version which to me looks 
cleaner than yours but as they say: "Beauty is in the eye of the beholder"

#---------------------------------------------------------
lineSize = 4
lsm1 = lineSize - 1

f = open('kaka.txt','r')
inData = f.read()
f.close()

inLst = inData.split()
inCount = len(inLst)
inLst += [' ']*lineSize
print inLst

f = open('kakaDump.txt','w')
for ind,elem in enumerate(inLst):
     if ind == inCount: break
     for i in range(lsm1): f.write('%s ' % inLst[ind + i])
     f.write('%s\n' % inLst[ind + lineSize])
f.close()
#---------------------------------------------------------

HTH,
Jussi



More information about the Python-list mailing list