I want to read data from a (very large, whitespace separated, two-column) text file into a Python dictionary. I tried to do this with a for-loop but that was too slow. MUCH faster is reading it with numpy loadtxt into a struct array and then converting it to a dictionary:
data = np.loadtxt('filename.txt', dtype=[('field1', 'a20'), ('field2', int)], ndmin=1)
result = dict(data)
But this is surely not the best way? Any advice?
The main reason I need something else, is that the following does not work:
data[0]['field1'].split(sep='-')
It leads to the error message:
TypeError: Type str doesn't support the buffer API
If the split() method exists, why can't I use it? Should I use a different dtype? Or is there a different (fast) way to read the text file? Is there anything else I am missing?
Versions: python version 3.3.2 numpy version 1.7.1
Edit:
changed data['field1'].split(sep='-')
to data[0]['field1'].split(sep='-')