Text files

Opening files

To open file a text file, use the with statement. It allows to insure that the file is properly opened and properly closed, even if an error is encountered (see the-with-statement for a detailed description).

# defining the name of the file to read
filename = 'data/nao.txt'

with open(filename,'r') as f:
    print(f.closed)
print(f.closed)
False
True

Reading

# read all the lines and store them in a list
with open(filename,'r') as f:
    lines = f.readlines()
print(len(lines))
print('@%s@' %lines[0])
print('#%s#' %lines[0].strip())  # removes the \n char
122
@ Hurrell PC-Based North Atlantic Oscillation Index (DJFM)
@
#Hurrell PC-Based North Atlantic Oscillation Index (DJFM)#
# Read all the file and store as 1 string
with open(filename,'r') as f:
    data = f.read()
print(len(data))
print(data[:10])
1994
 Hurrell P
# Read the first 200 chars of the file and store as 1 string
with open(filename,'r') as f:
    data = f.read(200)
print(len(data))
print(data)
200
 Hurrell PC-Based North Atlantic Oscillation Index (DJFM)
  1899.   -0.69
  1900.   -1.32
  1901.   -0.37
  1902.   -0.69
  1903.    1.33
  1904.   -0.01
  1905.    0.83
  1906.    0.83
  1907.    1.1
# loop over all the lines of the file, and extract the first one
# finishes when "end of line is found"
with open(filename,'r') as f:
    for line in f:
        print(line.strip())
Hurrell PC-Based North Atlantic Oscillation Index (DJFM)
1899.   -0.69
1900.   -1.32
1901.   -0.37
1902.   -0.69
1903.    1.33
1904.   -0.01
1905.    0.83
1906.    0.83
1907.    1.11
1908.    0.56
1909.   -0.50
1910.    0.43
1911.    0.47
1912.   -0.43
1913.    1.40
1914.    0.47
1915.   -0.70
1916.   -0.22
1917.   -1.18
1918.    0.03
1919.   -0.87
1920.    1.58
1921.    0.69
1922.    0.59
1923.    0.36
1924.   -0.65
1925.    1.09
1926.   -0.31
1927.    0.50
1928.    0.16
1929.   -0.59
1930.    0.42
1931.   -0.68
1932.   -0.09
1933.   -0.10
1934.    0.97
1935.    0.07
1936.   -1.94
1937.   -0.15
1938.    0.54
1939.   -0.13
1940.   -1.94
1941.   -1.23
1942.   -1.07
1943.    0.62
1944.    0.52
1945.   -0.08
1946.   -0.27
1947.   -1.86
1948.    0.26
1949.    1.02
1950.    0.48
1951.   -0.93
1952.   -0.14
1953.   -0.01
1954.    0.15
1955.   -1.25
1956.   -1.04
1957.    0.04
1958.   -1.26
1959.    0.10
1960.   -1.81
1961.    0.74
1962.   -0.92
1963.   -1.29
1964.   -0.88
1965.   -0.81
1966.   -1.56
1967.    0.62
1968.   -0.15
1969.   -2.57
1970.   -1.15
1971.   -0.66
1972.    0.12
1973.    1.20
1974.    0.37
1975.    0.74
1976.    1.22
1977.   -1.34
1978.   -0.55
1979.   -1.44
1980.   -0.35
1981.    0.29
1982.   -0.21
1983.    1.29
1984.    0.43
1985.   -0.80
1986.   -0.01
1987.   -0.66
1988.   -0.17
1989.    2.46
1990.    1.91
1991.    0.56
1992.    1.76
1993.    1.76
1994.    0.87
1995.    1.29
1996.   -1.44
1997.    0.71
1998.   -0.01
1999.    0.66
2000.    1.61
2001.   -1.01
2002.    0.62
2003.    0.05
2004.   -0.12
2005.    0.36
2006.   -0.90
2007.    1.16
2008.    1.06
2009.    0.04
2010.   -2.57
2011.   -0.68
2012.    1.60
2013.   -1.68
2014.    0.91
2015.    1.87
2016.    1.00
2017.    0.89
2018.   -0.16
2019.    0.76
# to parse a certain range of the file, one 
# way is to use the itertools package
import itertools
with open(filename,'r') as f:
    for line in itertools.islice(f, 5, 10):
        print(line.strip())
1903.    1.33
1904.   -0.01
1905.    0.83
1906.    0.83
1907.    1.11

Writting

Files are written out line by line.

# Generates some data
import numpy as np

xdata = np.linspace(0, np.pi/4., 5)
cosx = np.cos(xdata)
sinx = np.sin(xdata)
tanx = np.tan(xdata)
# opening the file
with open('data/outfile.txt', 'w') as fout:

    # writting the header: 4 strings separated by tabs.
    header = ['x', 'cos', 'sin', 'tan']
    string='%s\t%s\t%s\t%s\n' % (header[0], header[1], header[2], header[3])  # writes the header
    print(string)
    fout.write(string)


    # looping over all the data
    for x, c, s, t in zip(xdata, cosx, sinx, tanx):
        # writting the string associated with the data
        string = '%.4f\t%.8f\t%.8f\t%.8f\n' %(x, c, s, t)    # writes the data
        print(string)
        fout.write(string)
x	cos	sin	tan

0.0000	1.00000000	0.00000000	0.00000000

0.1963	0.98078528	0.19509032	0.19891237

0.3927	0.92387953	0.38268343	0.41421356

0.5890	0.83146961	0.55557023	0.66817864

0.7854	0.70710678	0.70710678	1.00000000