In [6]:
# We can create a file object and store it inside a variable.
# you can see objects as a different type of data
f=open("awanode-farmlab-2017-08-14.txt")
print(f)
It says that the file is opened, reminds the filename and indicates that it is in mode 'r', which means 'read'
You can call specific functions on an object with the syntax:
object.function()
Actually you already did this with lists and strings, for instance when doing str.split(" ")
In [14]:
f=open("awanode-farmlab-2017-08-14.txt")
# The read() function reads the content of a file and returns it in a (very long) string
s=f.read()
# Here we split the content of file by using a special character. The character \ (antislash) indicates a special
# character \n is understood as a single character tthat means 'newline'
# This will return an array of strings that are the lines of the file
lines=s.split("\n")
In [13]:
s
Out[13]:
In [15]:
# Let's peek at the first ten lines of the array
lines[2000:2010]
Out[15]:
In [16]:
# Let's examine a random line
l=lines[2401]
l
Out[16]:
Let's talk a bit about the kind of files we are examining here. It is an example of bad design of file format but a good exercise for interpreting in python. It is a set of records that our sensor box made over the course of 10 days. Each line is a record.
This line starts by a timestamp: 10th May 2001 (the clock was not setup appropriately) at 3:3:29.
Then there is a sequence number, this is the 1098th packet emitted by the node since its last restart.
Then there is a series of id=value elements that indicate the id of a sensor and the value read by the device.
In the end there is a \r special character that is a carriage return, which is another way of indicating the end of a line.
In [17]:
# Let's start by removing the rubish (special characters, whitespace) at
# the right of the string with the function rstrip()
l = l.rstrip()
l
Out[17]:
In [18]:
# This kind of format is bad design because there are many types of
# separators used | ; and =
# Let's replace all of them with ;
l = l.replace("|",";")
l = l.replace("=",";")
l
Out[18]:
In [19]:
# Finally, let's split that in an array:
l.split(";")
Out[19]:
In [20]:
# Notice that we could have compressed all these `l = l.something()` lines
# into a single line:
l=lines[2401]
l.rstrip().replace("|",";").replace("=",";").split(";")
Out[20]:
In [22]:
# We can also feed this array directly into variables using this convenient
# syntax:
a = l.rstrip().replace("|",";").replace("=",";").split(";")
(time,num, id1,val1, id2,val2, id3,val3, id4,val4, id5,val5, e) = a
In [29]:
int(val1)
Out[29]:
In [30]:
# Let's try with a common library
import math
In [31]:
# To call a modules' function, use this syntax:
math.sqrt(9)
Out[31]:
In [32]:
# If you are lazy, you can also import the library and give it a shorter name:
import math as m
m.sqrt(4)
Out[32]:
In [33]:
# Sometime you prefer to import a single function or object from your library
# In that case you specify it this way:
from math import sqrt
sqrt(5)
Out[33]:
In [ ]: