Source code for Exercises.essentials.file_io.ascii_log_file.ascii_log_file
"""
Read in a set of logs from an ASCII file.
Read in the logs found in the file `short_logs.crv`.
The logs are arranged as follows::
DEPTH S-SONIC P-SONIC ...
8922.0 171.7472 86.5657
8922.5 171.7398 86.5638
8923.0 171.7325 86.5619
8923.5 171.7287 86.5600
...
So the first line is a list of log names for each column of numbers.
The columns are the log values for the given log. Despite the forbidding
sounding extension (".crv"), `short_logs.crv` is just an
ASCII text file containing information organized as a table, in which the values
are all separated by spaces.
Make a dictionary with keys as the log names and values as the
log data::
>>> logs['DEPTH']
[8922.0, 8922.5, 8923.0, ...]
>>> logs['S-SONIC']
[171.7472, 171.7398, 171.7325, ...]
Bonus
~~~~~
Time your example using::
run -t 'ascii_log_file.py'
And see if you can come up with a faster solution. You may want to try the
`long_logs.crv` file in this same directory for timing, as it is much larger
than the `short_logs.crv` file. As a hint, reading the data portion of the
array in at one time combined with strided slicing of lists is useful here.
"""
from collections import defaultdict
[docs]def read_crv ():
"""
Write a for loop that loops through the
log file and fills the dictionary as described
above.
"""
log_file = open('short_logs.crv', 'r')
log_dict = defaultdict(list)
## Your code here
return log_dict
if __name__ == '__main__':
log_dict = read_crv()