In [1]:
%config IPCompleter.greedy = True
In [2]:
filename = 'data/example_read.txt'
fobj = open(filename)
In [3]:
content = fobj.read()
print(content)
Reading again will not show any content, because the current position is at the end of the file
In [4]:
content = fobj.read()
print(content)
So we have to seek it back to the start
In [5]:
fobj.seek(0)
content = fobj.read()
print(content)
In [6]:
fobj.close()
Always use the with statement. Closes the file at the end and in the case of an exeption.
In [7]:
filename = 'data/example_read.txt'
with open(filename) as fobj:
content = fobj.read()
print(content)
The default mode is read only 'r', we have to enable the writing mode 'w'.
In [8]:
with open('data/example_write.txt', mode='w') as fobj:
fobj.write('\t\tPython in the Lab\n')
fobj.write(content)
Quick check
In [9]:
with open('data/example_write.txt') as fobj:
print(fobj.read())
In [10]:
with open('data/example_write.txt') as fobj:
print(fobj.readline(), end='')
print(fobj.readline(), end='')
print(fobj.readline(), end='')
print(fobj.readline(), end='')
print(fobj.readline(), end='')
print(fobj.readline(), end='')
print(fobj.readline(), end='')
print(fobj.readline(), end='')
print(fobj.readline(), end='')
print(fobj.readline(), end='')
print(fobj.readline(), end='')
print(fobj.readline(), end='')
print(fobj.readline(), end='')
In [11]:
with open('data/example_write.txt') as fobj:
for line in fobj:
print(line, end='')
Or read all lines into a python list first
In [12]:
with open('data/example_write.txt') as fobj:
lines = fobj.readlines()
for line in reversed(lines):
print(line, end='')
This for example would allow is to slice it later
In [13]:
for line in lines[2:6]:
print(line, end='')
For practice we will only use modules of the Python standart libary in this example. Later we make everything easier with numpy.
In [14]:
import math # Python math module
import random # Python random module
Define noisy sin und cos function
In [15]:
def noisy_sin(phi, amp=1, noise_amp=0.25):
return amp * math.sin(phi) + noise_amp * random.random()
def noisy_cos(phi, amp=1, noise_amp=0.25):
return amp * math.cos(phi) + noise_amp * random.random()
Use list comprehension to create data lists
In [16]:
phi_data = [4 * math.pi *(x / 500) for x in range(0, 250)]
sin_data = [noisy_sin(phi, noise_amp=0.4) for phi in phi_data]
cos_data = [noisy_cos(phi, amp=1.5, noise_amp=0.2) for phi in phi_data]
Plot everything
In [17]:
import matplotlib.pyplot as plt
%matplotlib inline
In [18]:
plt.figure(figsize=(12, 4))
plt.plot(phi_data, sin_data, color='red', marker='x')
plt.plot(phi_data, cos_data, 'g.')
Out[18]:
Let's write some data to file
In [19]:
with open('data/data_write.txt', 'w') as fobj:
for datapoint in sin_data:
fobj.write('{:.3f}\n'.format(datapoint))
How do we write all three data next to each other?
In [20]:
with open('data/data_write.txt', 'w') as fobj:
for i in range(len(phi_data)):
fstr = '{:.3f}; {:.3f}; {:.3f} \n'
fobj.write(fstr.format(phi_data[i], sin_data[i], cos_data[i]))
When ever you use indices in Python you are doing something WRONG or you EXACTLY know why.
Just zip it
In [21]:
with open('data/data_write.txt', 'w') as fobj:
for phi, sin, cos in zip(phi_data, sin_data, cos_data):
fstr = '{:.3f}; {:.3f}; {:.3f} \n'
fobj.write(fstr.format(phi, sin, cos))
A few word on zip
In [22]:
l0 = [1, 2, 3]
l1 = ['a', 'b', 'c']
l2 = ['#', '*', '%']
In [23]:
for item in zip(l0, l1 ,l2):
print(item)
There are more little helper products product(), chain(), cycle(), repeat(), ...... in iterttools
Now let us add some head data
In [24]:
import datetime
In [25]:
with open('data/data_write.txt', 'w') as fobj:
# Write header
fobj.write('#Python in the Lab\n')
fobj.write('#\n')
fobj.write('#{} \n'.format(datetime.datetime.now()))
fobj.write('#\n')
fobj.write('#Writing some noisy sin and cos data to a file.\n')
fobj.write('\n')
fobj.write('\n')
fobj.write('phi (2pi); sin (a.u.); cos (a.u.)\n')
# Write data
for phi, sin, cos in zip(phi_data, sin_data, cos_data):
fstr = '{:.3f}; {:.3f}; {:.3f} \n'
fobj.write(fstr.format(phi, sin, cos))
Some words on datetime objects
In [26]:
now = datetime.datetime.now()
now
Out[26]:
In [27]:
print(now)
In [28]:
print('Year:', datetime.datetime.now().year)
print('Month:', datetime.datetime.now().month)
print('Day:', datetime.datetime.now().day)
print('Hour:', datetime.datetime.now().hour)
print('Minute:', datetime.datetime.now().minute)
print('Second:', datetime.datetime.now().second)
print('Microsecond: ',datetime.datetime.now().microsecond)
Feel free to format the date as you like. For a complete list of formatting directives, see strftime() and strptime() Behavior.
In [29]:
print('{:%a %H:%M %d.%m.%Y}'.format(now))
print('{:%A %H:%M %d %B %Y %z}'.format(now))
print('{:%A %I %P:%M %d.%b.%y}'.format(now))
print('The day is {0:%d}, the month is {0:%B}.'.format(now))
In [30]:
# Read data in lines from file
with open('data/data_write.txt') as fobj:
lines = fobj.readlines()
# Organize everythin nicly in a dict
data = {}
data['title'] = lines[0].strip('#').strip().rstrip()
data['time'] = lines[2].strip('#').strip().rstrip()
data['comment'] = lines[4].strip('#').strip().rstrip()
data['phi'] = []
data['sin'] = []
data['cos'] = []
for dataline in lines[8:]:
# Remove linebreaks '\n' and split the line
phi, sin, cos = dataline.strip().split(';')
data['phi'].append(float(phi))
data['sin'].append(float(sin))
data['cos'].append(float(cos))
In [31]:
fig, axs = plt.subplots(2, 1, figsize=(12, 6))
axs[0].plot(data['phi'], data['sin'], color='red', marker='x')
axs[1].plot(data['phi'], data['cos'], 'g.')
for ax in axs:
ax.set_xlim(0, 2 * math.pi)
ax.set_ylim(-1.8, 1.8)
In [32]:
import numpy as np
In [33]:
data = {}
data['phi'], data['sin'], data['cos'] = np.genfromtxt('data/data_write.txt', skip_header=8, delimiter=';').T
In [34]:
plt.figure(figsize=(12, 4))
plt.plot(data['phi'], data['sin'], color='red', marker='x')
plt.plot(data['phi'], data['cos'], 'g.')
Out[34]:
Or more advanced use a custom dtype
In [35]:
dt = np.dtype([('phi', 'f8'), ('sin', 'f8'), ('cos', 'f8')])
data = np.genfromtxt('data/data_write.txt', dtype=dt, skip_header=8, delimiter=';').T
In [36]:
fig, axs = plt.subplots(1, 2, figsize=(12, 3))
axs[0].plot(data['phi'], data['sin'], color='red', marker='x')
axs[1].plot(data['phi'], data['cos'], 'g.')
Out[36]: