I am trying to read a large txt file (1.6 GB) in order to store its content in dictionaries. I am facing a hard time reading the file and it takes a lot to be over with. Actually I don´t know the exact time required to run the whole code because I stopped after 10 min of waiting :(.
Here is the code:
import numpy as np
import pylab as pl
import matplotlib.pyplot as plt
import fileinput
import time
def extractdata2():
start_time = time.time()
accel_data = { 'timestamp': [], 'sensor': [], 'x': [], 'y': [], 'z': [] }
accel_uncalib_data = { 'timestamp': [], 'sensor': [], 'x_uncalib': [], 'y_uncalib': [], 'z_uncalib': [], 'x_bias': [], 'y_bias': [], 'z_bias': [] }
gyro_data = { 'timestamp': [], 'sensor': [], 'x': [], 'y': [], 'z': []}
gyro_uncalib_data = { 'timestamp': [], 'sensor': [], 'x_uncalib': [], 'y_uncalib': [],
'z_uncalib': [], 'x_drift': [], 'y_drift': [], 'z_drift': []}
magnet_data = { 'timestamp': [], 'sensor': [], 'x': [], 'y': [], 'z': [] }
magnet_uncalib_data = { 'timestamp': [], 'sensor': [], 'x_uncalib': [], 'y_uncalib': [], 'z_uncalib': [], 'x_bias': [], 'y_bias': [], 'z_bias': []}
with open("accelerometer.txt") as myfile:
for line in myfile:
line = line.split(',')
if "TYPE_ACCELEROMETER" in line:
#IMU_data["accel_data"] = line # the line must be split in 4 camps
accel_data["timestamp"].append(line[ 0 ] )
accel_data["sensor"].append( line[ 1 ] )
accel_data["x"].append( line[ 2 ] )
accel_data["y"].append( line[ 3 ] )
accel_data["z"].append( line[ 4 ] )
#print(accel_data)
elif "TYPE_ACCELEROMETER_UNCALIBRATED" in line:
accel_uncalib_data["timestamp"].append( line[ 0 ] )
accel_uncalib_data["sensor"].append( line[ 1 ] )
accel_uncalib_data["x_uncalib"].append( line[ 2 ] )
accel_uncalib_data["y_uncalib"].append( line[ 3 ] )
accel_uncalib_data["z_uncalib"].append( line[ 4 ] )
accel_uncalib_data["x_bias"].append( line[ 5 ] )
accel_uncalib_data["y_bias"].append( line[ 6 ] )
accel_uncalib_data["z_bias"].append( line[ 7 ] )
#print(accel_uncalib_data)
elif "TYPE_GYROSCOPE" in line:
gyro_data["timestamp"].append( line[ 0 ] )
gyro_data["sensor"].append( line[ 1 ] )
gyro_data["x"].append( line[ 2 ] )
gyro_data["y"].append( line[ 3 ] )
gyro_data["z"].append( line[ 4 ] )
#print(gyro_data)
elif "TYPE_GYROSCOPE_UNCALIBRATED" in line:
gyro_uncalib_data["timestamp"].append( line[ 0 ] )
gyro_uncalib_data["sensor"].append( line[ 1 ] )
gyro_uncalib_data["x_uncalib"].append( line[ 2 ] )
gyro_uncalib_data["y_uncalib"].append( line[ 3 ] )
gyro_uncalib_data["z_uncalib"].append( line[ 4 ] )
gyro_uncalib_data["x_drift"].append( line[ 5 ] )
gyro_uncalib_data["y_drift"].append( line[ 6 ] )
gyro_uncalib_data["z_drift"].append( line[ 7 ] )
#print(gyro_uncalib_data)
elif "TYPE_MAGNETIC_FIELD" in line:
magnet_data["timestamp"].append( line[ 0 ] )
magnet_data["sensor"].append( line[ 1 ] )
magnet_data["x"].append( line[ 2 ] )
magnet_data["y"].append( line[ 3 ] )
magnet_data["z"].append( line[ 4 ] )
#print(magnet_data)
elif "TYPE_MAGNETIC_FIELD_UNCALIBRATED" in line:
magnet_uncalib_data["timestamp"].append( line[ 0 ] )
magnet_uncalib_data["sensor"].append( line[ 1 ] )
magnet_uncalib_data["x_uncalib"].append( line[ 2 ] )
magnet_uncalib_data["y_uncalib"].append( line[ 3 ] )
magnet_uncalib_data["z_uncalib"].append( line[ 4 ] )
magnet_uncalib_data["x_bias"].append( line[ 5 ] )
magnet_uncalib_data["y_bias"].append( line[ 6 ] )
magnet_uncalib_data["z_bias"].append( line[ 7 ] )
#print(magnet_uncalib_data)
print("--- %s seconds ---" % (time.time() - start_time))
return accel_data, accel_uncalib_data, gyro_data, gyro_uncalib_data, magnet_data, magnet_uncalib_data
How can I speed up my routine? I have tried many of the types mentioned on stackoverflow in similar cases but it didn´t work.
Many thanks in advance! :)