
Hi, How do I write a loadtxt command to read in the following file and store each data point as the appropriate data type: 12|h|34.5|44.5 14552|bbb|34.5|42.5 Do the strings have to be read in separately from the numbers? Why would anyone use 'S10' instead of 'string'? dt = {'names': ('gender','age','weight','bal'), 'formats': ('i4', 'S4','f4', 'f4')} a = loadtxt("sample_data.txt", dtype=dt) gives ValueError: need more than 1 value to unpack I can do a = loadtxt("sample_data.txt", dtype="string") but can't use 'string' instead of S4 and all my data is read into strings. Seems like all the examples on-line use either numeric or textual input, but not both. Thanks.

On Tue, Feb 10, 2009 at 9:40 PM, A B <python6009@gmail.com> wrote:
Hi,
How do I write a loadtxt command to read in the following file and store each data point as the appropriate data type:
12|h|34.5|44.5 14552|bbb|34.5|42.5
Do the strings have to be read in separately from the numbers?
Why would anyone use 'S10' instead of 'string'?
dt = {'names': ('gender','age','weight','bal'), 'formats': ('i4', 'S4','f4', 'f4')}
a = loadtxt("sample_data.txt", dtype=dt)
gives
ValueError: need more than 1 value to unpack
I can do a = loadtxt("sample_data.txt", dtype="string") but can't use 'string' instead of S4 and all my data is read into strings.
Seems like all the examples on-line use either numeric or textual input, but not both.
Thanks. _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
works for me but not sure i understand the problem, did you try setting the delimiter? import numpy as np from cStringIO import StringIO txt = StringIO("""\ 12|h|34.5|44.5 14552|bbb|34.5|42.5""") dt = {'names': ('gender','age','weight','bal'), 'formats': ('i4', 'S4','f4', 'f4')} a = np.loadtxt(txt, dtype=dt, delimiter="|") print a.dtype

On Tue, Feb 10, 2009 at 9:52 PM, Brent Pedersen <bpederse@gmail.com> wrote:
On Tue, Feb 10, 2009 at 9:40 PM, A B <python6009@gmail.com> wrote:
Hi,
How do I write a loadtxt command to read in the following file and store each data point as the appropriate data type:
12|h|34.5|44.5 14552|bbb|34.5|42.5
Do the strings have to be read in separately from the numbers?
Why would anyone use 'S10' instead of 'string'?
dt = {'names': ('gender','age','weight','bal'), 'formats': ('i4', 'S4','f4', 'f4')}
a = loadtxt("sample_data.txt", dtype=dt)
gives
ValueError: need more than 1 value to unpack
I can do a = loadtxt("sample_data.txt", dtype="string") but can't use 'string' instead of S4 and all my data is read into strings.
Seems like all the examples on-line use either numeric or textual input, but not both.
Thanks. _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
works for me but not sure i understand the problem, did you try setting the delimiter?
import numpy as np from cStringIO import StringIO
txt = StringIO("""\ 12|h|34.5|44.5 14552|bbb|34.5|42.5""")
dt = {'names': ('gender','age','weight','bal'), 'formats': ('i4', 'S4','f4', 'f4')} a = np.loadtxt(txt, dtype=dt, delimiter="|") print a.dtype
I had tried both with and without the delimiter. In any event, it just worked for me as well. Not sure what I was missing before. Anyway, thank you.

On Wed, Feb 11, 2009 at 6:27 PM, A B <python6009@gmail.com> wrote:
On Tue, Feb 10, 2009 at 9:52 PM, Brent Pedersen <bpederse@gmail.com> wrote:
On Tue, Feb 10, 2009 at 9:40 PM, A B <python6009@gmail.com> wrote:
Hi,
How do I write a loadtxt command to read in the following file and store each data point as the appropriate data type:
12|h|34.5|44.5 14552|bbb|34.5|42.5
Do the strings have to be read in separately from the numbers?
Why would anyone use 'S10' instead of 'string'?
dt = {'names': ('gender','age','weight','bal'), 'formats': ('i4', 'S4','f4', 'f4')}
a = loadtxt("sample_data.txt", dtype=dt)
gives
ValueError: need more than 1 value to unpack
I can do a = loadtxt("sample_data.txt", dtype="string") but can't use 'string' instead of S4 and all my data is read into strings.
Seems like all the examples on-line use either numeric or textual input, but not both.
Thanks. _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
works for me but not sure i understand the problem, did you try setting the delimiter?
import numpy as np from cStringIO import StringIO
txt = StringIO("""\ 12|h|34.5|44.5 14552|bbb|34.5|42.5""")
dt = {'names': ('gender','age','weight','bal'), 'formats': ('i4', 'S4','f4', 'f4')} a = np.loadtxt(txt, dtype=dt, delimiter="|") print a.dtype
I had tried both with and without the delimiter. In any event, it just worked for me as well. Not sure what I was missing before. Anyway, thank you.
Actually, I was using two different machines and it appears that the version of numpy available on Ubuntu is seriously out of date (1.0.4). Wonder why ... Version 1.2.1 on a RedHat box worked fine.

2009/2/12 A B <python6009@gmail.com>: Actually, I was using two different machines and it appears that the version of numpy available on Ubuntu is seriously out of date (1.0.4). Wonder why ...
See the recent post here http://projects.scipy.org/pipermail/numpy-discussion/2009-February/040252.ht... Cheers, Scott

On 2/11/2009 6:40 AM, A B wrote:
Hi,
How do I write a loadtxt command to read in the following file and store each data point as the appropriate data type:
12|h|34.5|44.5 14552|bbb|34.5|42.5
dt = {'names': ('gender','age','weight','bal'), 'formats': ('i4', 'S4','f4', 'f4')}
Does this work for you? dt = {'names': ('gender','age','weight','bal'), 'formats': ('i4','S4','f4', 'f4')} with open('filename.txt', 'rt') as file: linelst = [line.strip('\n').split('|') for line in file] n = len(linelst) data = numpy.zeros(n, dtype=numpy.dtype(dt)) for i,(gender, age, weight, bal) in zip(range(n),linelst): data[i] = (int(gender), age, float(weight), float(bal)) S.M.
participants (4)
-
A B
-
Brent Pedersen
-
Scott Sinclair
-
Sturla Molden