file io (lagged values) newbie question

hiro NunezD at gmail.com
Tue Feb 20 01:17:42 EST 2007


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:
-----------------------------------------------------------------------------------------------------------
array = ['1','2','3','4','5','6','7']
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):
               #print "first loop"
               #print
               if indexNum > (len(array) - 1):
                       break
               else:
                       line.append(array[indexNum])

       #print "startIndex"
       #print startIndex + skip

       for indexNum in range(startIndex + skip, (startIndex +
lineSize) + 1, 1):
               #print "second loop"
               #print
               if indexNum > (len(array) - 1):
                       break
               else:
                       line.append(array[indexNum])
       print line

----------------------------------------------------------------------------------------------------------------------
which outputs to the console:

['1', '2', '3', '5']
['2', '3', '4', '6']
['3', '4', '5', '7']
['4', '5', '6']
['5', '6', '7']
['6', '7']
['7']

This is exactly what I want and need, but when modified to read and
write files from/to disk, I run into problems.
example text file for reading:

C:\>more kaka.txt
1
2
3
4
5
6
7

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.




More information about the Python-list mailing list