File I/O with arrays; plain ASCII format

  • Plain text output to file (just dump repr(array)):
    a = linspace(1, 21, 21); a.shape = (2,10)
    
    file = open('tmp.dat', 'w')
    file.write('Here is an array a:\n')
    file.write(repr(a))   # dump string representation of a
    file.close()
    

  • Plain text input (just take eval on input line):
    file = open('tmp.dat', 'r')
    file.readline()  # load the first line (a comment)
    b = eval(file.read())
    file.close()
    

    previousnexttable of contents