Creating a python dictionary in python from tab delimited text file with headers as keywords
<p>I am rather new to Python and am having trouble trying to create a function which reads in tab deliminated text files and creats a dictionary from the data. I am mostly dealing with text files of the following format with a number of tab deliminated numerical data columns with corresponding headers for each column:</p>

<pre><code>Time_(s)  Mass_Flow_(kg/s)  T_in_pipe(C)  T_in_water(C)  T_out_pipe(C)  T_out_water(C)

0    1.2450   16.9029   16.8256   16.6234   16.6204
    2.8700    1.2450   16.8873   16.8094   16.6237   19.6507
    5.6600    1.2450   16.8889   16.8229   19.1406   29.1320
    8.7800    1.2450   16.8875   16.8236   24.1325   34.9077
   11.6200    1.2450   16.8794   16.8040   28.3927   38.5443
   16.0600    1.2450   16.8615   16.7942   33.7205   42.4149
   18.8900    1.2450   16.8512   16.7938   36.2797   44.1221
   23.0200    1.2450   16.8319   16.7903   39.2102   46.1857
   25.7600    1.2450   16.8380   16.7952   40.7243   47.2657
</code></pre>

<p>Preferably, I want to write a code which stores each column of data as an array but also to store headings of each column into a seperate array so that I can use them as keywords in a dictionary. For example, if I lookup the dictionary key "Mass_Flow_(kg/s)", an array would be returned of all the values in the mass flow rate column (excluding the header). </p>

<p>So far I have tried using numpy.loadtxt to create such numerical arrays from the columns but I have not been successful in extracting the header data and thus have had to skip this line. The following code will produce the dictionary I want but I would rather a more flexible code which doesn't require me to manually name each of the columns despite the names already being contained within the .txt file. </p>

<pre><code>import numpy as np


time, m_flow, Tin_pipe, Tin_water, Tout_pipe, Tout_water = np.loadtxt("pipeData.txt",skiprows=1,unpack=True) 

#Assign each column in file to respective arrays

my_dict = {"Time":time, "Mass flow rate":m_flow, "Tin_pipe":Tin_pipe, "Tin_water":Tin_water, "Tout_pipe":Tout_pipe, "Tout_water":Tout_water}
#Line arrays to keywords and merge into a dictionary
</code></pre>

<p>I have tried not skipping the first row but loadtxt usually returns wih:</p>

<pre><code>ValueError: could not convert string to float: Time_(s)
</code></pre>

<p>Therefore I think I need to use another module if I want to read both the string data and numerical values. If anybody has any suggestions of how I may go about doing this or knows of a better module for doing this it would be greatly appreciated. </p>

<p>Keith</p>

<p>Take a look at the <a href="http://pandas.pydata.org/" rel="nofollow">Pandas module</a></p>

<pre><code># This module kicks ass
import pandas as pd

pipe_data = pd.read_csv('pipeData.txt', sep='\t')

print pipe_data.columns # prints Time_(s), Mass_Flow_(kg/s), ...

print pipe_data['Time_(s)'] # print the Time_(s) column
</code></pre>

python arrays numpy dictionary text