[Tutor] Writing to file

Guillermo Fernandez guillermo.fernandez at epfl.ch
Mon Dec 8 17:28:37 EST 2003


hi,

I'm trying to making Octave (later will be Matlab...) and one of my programs 
communicate together. For that, I create a file that's similar to the one that 
Octave creates to save his variables (with the 'save' command). This look like 
this:
# Created by Octave 2.1.42, Mon Dec 08 18:57:00 2003 RST <guille at Guille>
# name: a
# type: matrix
# rows: 2
# columns: 2
  1 2
  3 4

I did the program and the file is correctly created. Still, while loading my 
file, I've got this error:
octave:1> load wireless
' found in file `wireless'ier `daysmac

I'm almost sure the difference between the files he creates and I create is the 
following:
$ file wireless
wireless: ASCII text, with CRLF line terminators
$ file save
save: ASCII text

I don't really get what's those CRLF line terminators (maybe the '\r\n' of 
windows?) but I would apreciate to get rid of them to have standard ASCII file 
And as the newlines attribute of a file is not writable, I can not set it to 
'\n' (seems to be set to None for the moment).

Hereafter is my code.

Thanks,

Guille

"""
Matlab test and plot functions. it takes information from the database
and translates it to files understandable by Matlab and Octave.

For any comment, criticism, or whatever, feel free to contact me:
guillermo.fernandez at epfl.ch
"""

import begohandler # That's one library of mine that returns
                    # lists of dictionaries and so on.
import time

def mat_apmaclist(aplist,data,conditions=None):
     result=begohandler.get_apmaclist(aplist,conditions)
     data.write('# name: mac\n')
     data.write('# type: matrix\n')
     data.write('# rows: '+str(len(result))+'\n')
     data.write('# columns: 1\n')
     for key in result:
         data.write(' '+str(len(result[key]))+'\n')

def mat_macaplist(maclist,data,conditions=None):
     result=begohandler.get_macaplist(maclist,conditions)
     data.write('# name: ap\n')
     data.write('# type: matrix\n')
     data.write('# rows: '+str(len(result))+'\n')
     data.write('# columns: 1\n')
     for key in result:
         data.write(' '+str(len(result[key]))+'\n')


def mat_daysmacstat(data,conditions=None):
     result=begohandler.get_daysmacstat(conditions)
     data.write('# name: daysmac\n')
     data.write('# type: matrix\n')
     data.write('# rows: '+str(len(result))+'\n')
     data.write('# columns: 2\n')
     for key in result:
         data.write(' '+str(key)+' '+str(result[key])+'\n')


#--- Main function
if __name__=='__main__':
     begohandler.do_opendb('large.db')
     data=file('wireless','w')
     data.write('# Created by BEGO, '+time.strftime('%a %m %d %H:%M:%S %Y 
%Z')+'\n')
     # mat_apmaclist(['%'],data)
     # mat_macaplist(['%'],data)
     mat_daysmacstat(data)
     data.close()
     begohandler.do_closedb()






More information about the Tutor mailing list