I have data which has a very particular format that I'm trying to store in a numpy array.

avi_05MSalt00001.txt 2012 Feb 07 20:12:41 50000 (usec) 300 10

I've defined the following numpy datatype:

timefile_dtype=numpy.dtype([
	     ('filename', file),
   	     ('year', int),
	     ('month', str),  #Why don't these work?
	     ('day', int),
 	     ('time', str),
	     ('int_time', int),
	     ('unit', str),
	     ('avg', int),
	     ('boxcar', int),
			])

The output looks like this:'

('avidin_in_water00349.txt', 2012, '', 29, '', 50000, '', 300, 10)

Is it possible for numpy to store these string variables? If so, am I doing something wrong? I like the swiftness of this method rather than making my own list of lists...

Thanks.

Recommended Answers

All 2 Replies

I think you must specify a length for the strings, otherwise the length defaults to zero.

import numpy

timefile_dtype=numpy.dtype([
         ('filename', file),
         ('year', int),
         ('month', str, 25),
         ('day', int),
         ('time', str, 25),
         ('int_time', int),
         ('unit', str, 25),
         ('avg', int),
         ('boxcar', int),
            ])

a = numpy.zeros((1,), dtype=timefile_dtype)
x = a[0]

x['filename'] = "hello.txt"
x['month'] = 'August'

print x.dtype
print x

""" my output -->
[('filename', '|O8'), ('year', '<i8'), ('month', '|S25'), ('day', '<i8'), ('time', '|S25'), ('int_time', '<i8'), ('unit', '|S25'), ('avg', '<i8'), ('boxcar', '<i8')]
('hello.txt', 0, 'August', 0, '', 0, '', 0, 0)
"""

Thanks for your help. I had forgotten about this thread until I came back to the problem. This fixed my problem! I'm overdeclaring the string space because I can't always anticipate how long the string will be, but this doesn't seem to post any problem except probably just wasting a bit of memory. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.