This will read all into a bunch of lists making them very easy to work with:
import os
import re
# Make sure path is set to current directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))
def read_data(filename):
with open(filename) as fp:
lines = fp.read().split('\n')
for n, line in enumerate(lines):
line = line.strip()
line = re.sub(' +',' ', line)
# header special case
if n == 0:
columns = line.split('\t')
else:
columns = line.split()
print(f"{columns}")
if __name__ == '__main__':
read_data('example_linearity.txt')
results:
Output:
['Freq(Hz)', 'Class 1 + (dB)', 'Class 1 - (dB)', 'Class 2 + (dB)', 'Class 2 - (dB)', 'voltage (mVrms)', 'NI voltage (mVrms)', 'SLM (dB)', 'NI SLM (dB)', 'Input (dB)', 'Expected SPL (dB', 'overload']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '76.0000', '0.0000', '94.0000', '94.3497', '94.0000', '94.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '135.1492', '0.0000', '99.0000', '99.3496', '99.0000', '99.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '240.3331', '0.0000', '104.0000', '104.3490', '104.0000', '104.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '427.3794', '0.0000', '109.0000', '109.3531', '109.0000', '109.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '760.0000', '0.0000', '114.0000', '114.3511', '114.0000', '114.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '852.7340', '0.0000', '115.0000', '115.1273', '115.0000', '115.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '956.7833', '0.0000', '116.0000', '116.3524', '116.0000', '116.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '1073.5285', '0.0000', '117.0000', '117.3523', '117.0000', '117.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '1204.5188', '0.0000', '118.0000', '118.3528', '118.0000', '118.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '42.7379', '0.0000', '89.0000', '89.3501', '89.0000', '89.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '24.0333', '0.0000', '84.0000', '84.3497', '84.0000', '84.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '13.5149', '0.0000', '79.0000', '79.3494', '79.0000', '79.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '7.6000', '0.0000', '74.0000', '74.3495', '74.0000', '74.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '4.2738', '0.0000', '69.0000', '69.3492', '69.0000', '69.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '2.4033', '0.0000', '64.0000', '64.3489', '64.0000', '64.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '1.3515', '0.0000', '59.0000', '59.3489', '59.0000', '59.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.7600', '0.0000', '54.0000', '54.3481', '54.0000', '54.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.4274', '0.0000', '49.1000', '49.3496', '49.0000', '49.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.2403', '0.0000', '44.1000', '44.3487', '44.0000', '44.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.1351', '0.0000', '39.1000', '39.3472', '39.0000', '39.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.0760', '0.0000', '34.3000', '34.3524', '34.0000', '34.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.0760', '0.0000', '34.3000', '34.3504', '34.0000', '34.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.0677', '0.0000', '33.3000', '33.3623', '33.0000', '33.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.0604', '0.0000', '32.4000', '32.3638', '32.0000', '32.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.0538', '0.0000', '31.5000', '31.3587', '31.0000', '31.0000', '0.0000']
['8000.0000', '1.1000', '-1.1000', '1.4000', '-1.4000', '0.0480', '0.0000', '30.5000', '30.3642', '30.0000', '30.0000', '1.0000']