Source code for data
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Read and write data to or from file.
.. module:: data
:platform: *nix, Windows
:synopsis: Handle data files.
.. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de>
"""
from __future__ import print_function
import pickle
[docs]def data_read(file_name, x_column, y_column):
"""Read ascii data file.
:param filename: file to read
:type filename: str
:param x_column: column index for the x data (first column is 0)
:type x_column: int
:param y_column: column index for the y data (first column is 0)
:type y_column: int
:returns: x and y
:rtype: tuple(list, list)
"""
import re
file = open(file_name)
x = []
y = []
for row in file:
fields = re.split(r'\s+', row.strip())
#print(filds)
x.append(float(fields[x_column]))
y.append(float(fields[y_column]))
file.close()
return x, y
[docs]def data_load(file_name, verbose=False):
"""Load stored program objects from binary file.
:param file_name: file to load
:type file_name: str
:param verbose: verbose information (default = False)
:type verbose: bool
:returns: loaded data
:rtype: object
"""
if verbose:
print('check if data is available')
try:
with open(file_name, 'rb') as input:
object_data = pickle.load(input) # one load for every dump is needed to load all the data
if verbose:
print('found:')
print(object_data)
except IOError:
object_data = None
if verbose:
print('no saved datas found')
return object_data
[docs]def data_store(file_name, object_data):
"""Store program objects to binary file.
:param file_name: file to store
:type file_name: str
:param object_data: data to store
:type object_data: object
"""
with open(file_name, 'wb') as output:
pickle.dump(object_data, output, pickle.HIGHEST_PROTOCOL) # every dump needs a load
[docs]def main():
"""Main function."""
file_name = "slit_test_scan.dat"
x, y = data_read(file_name, 3, 2)
print(x)
print(y)