Differences between revisions 3 and 4
Revision 3 as of 2008-04-16 17:45:35
Size: 710
Editor: anonymous
Comment:
Revision 4 as of 2008-04-16 17:52:48
Size: 904
Editor: anonymous
Comment:
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:
The file object is used for reading and writing plain text as well as unformatted binary data. The following Codeline
writes a message in the file with the name ''out.txt''
The [[http://docs.python.org/lib/bltin-file-objects.html|file object]] can be used for reading and
writing plain text as well as unformatted binary data. The following code
writes a message in the file with the name ''out.txt'', reads and print the data
Line 17: Line 18:
file('out.txt','wb').write('Hallo Datentraeger') file('out.txt','w').write('Hallo Datentraeger')
print file('out.txt').read()
Line 20: Line 22:
The data can be read in with the method '''.read()'''
{{{#!python
s=file('out.txt').read()
print s
'Hallo Datentraeger'
}}}
 * {{{write()}}} writes a string to the file
 * {{{read()}}} reads complete file
 * {{{read(N)}}} reads N bytes
 * {{{readlines()}}} reads the file with linebreaks
 * {{{readline()}}} reads only the next line

SiaProgrammingPython

Input/Output

This lesson deals with the ways of reading and writing data

Basic Python

The file object can be used for reading and writing plain text as well as unformatted binary data. The following code writes a message in the file with the name out.txt, reads and print the data

   1 file('out.txt','w').write('Hallo Datentraeger')
   2 print file('out.txt').read()
  • write() writes a string to the file

  • read() reads complete file

  • read(N) reads N bytes

  • readlines() reads the file with linebreaks

  • readline() reads only the next line

NumPy/SciPy

HDF

netCDF

Various Satellite data formats

LehreWiki: SiaProgrammingPythonIo (last edited 2008-04-21 12:10:37 by anonymous)