From: Ian Hoffman on
Hello,

I'm having significant Python difficulties (and I'm new to Python).
I'm trying to read BLOB ASCII (numerical) data from a MySQL database
using MySQLdb in a formatted fashion. The BLOB data is a sequence of
numbers separated by newlines (\n), like this:
5
6
10
45
etc.

When I read the data using the fetchone() command I get a single
tuple. What I'd like is to somehow put the tuple into a NumPy array
with each value as one element. Then I can continue to do some
numerical processing.

Any advice/help?

From: Matteo Landi on
I know anything about mysqldb and fetchone method, but it's easy to
create a numpy array, given a tuple of data:

>>> import numpy
>>>
>>> t = ('1', '2', '3')
>>> numpy.array(t, int)
array([1, 2, 3])
>>>

I made the assumption that mysqldb.fetchone return a tuple of strings,
so we need to create an array by specifying the type of the needed
values.

On Mon, May 24, 2010 at 12:30 AM, Ian Hoffman <ith140(a)gmail.com> wrote:
> Hello,
>
> I'm having significant Python difficulties (and I'm new to Python).
> I'm trying to read BLOB ASCII (numerical) data from a MySQL database
> using MySQLdb in a formatted fashion.  The BLOB data is a sequence of
> numbers separated by newlines (\n), like this:
> 5
> 6
> 10
> 45
> etc.
>
> When I read the data using the fetchone() command I get a single
> tuple.  What I'd like is to somehow put the tuple into a NumPy array
> with each value as one element.  Then I can continue to do some
> numerical processing.
>
> Any advice/help?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
Matteo Landi
http://www.matteolandi.net/
From: Ian Hoffman on
On May 23, 6:54 pm, Matteo Landi <landima...(a)gmail.com> wrote:
> I know anything about mysqldb and fetchone method, but it's easy to
> create a numpy array, given a tuple of data:
>
>
>
> >>> import numpy
>
> >>> t = ('1', '2', '3')
> >>> numpy.array(t, int)
> array([1, 2, 3])
>
> I made the assumption that mysqldb.fetchone return a tuple of strings,
> so we need to create an array by specifying the type of the needed
> values.
>
>
>
> On Mon, May 24, 2010 at 12:30 AM, Ian Hoffman <ith...(a)gmail.com> wrote:
> > Hello,
>
> > I'm having significant Python difficulties (and I'm new to Python).
> > I'm trying to read BLOB ASCII (numerical) data from a MySQL database
> > using MySQLdb in a formatted fashion.  The BLOB data is a sequence of
> > numbers separated by newlines (\n), like this:
> > 5
> > 6
> > 10
> > 45
> > etc.
>
> > When I read the data using the fetchone() command I get a single
> > tuple.  What I'd like is to somehow put the tuple into a NumPy array
> > with each value as one element.  Then I can continue to do some
> > numerical processing.
>
> > Any advice/help?
>
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> --
> Matteo Landihttp://www.matteolandi.net/

The problem is the tuple is contained in a single value separated by
newlines (only a[0] has a record), otherwise I could do as you
suggest...

Isn
From: Ian Hoffman on
On May 24, 2:11 am, Dennis Lee Bieber <wlfr...(a)ix.netcom.com> wrote:
> On Sun, 23 May 2010 21:44:30 -0700 (PDT), Ian Hoffman <ith...(a)gmail.com>
> declaimed the following in gmane.comp.python.general:
>
> > The problem is the tuple is contained in a single value separated by
> > newlines (only a[0] has a record), otherwise I could do as you
> > suggest...
>
> >>> blob = "1\n2\n3\n4\n"
> >>> tple = (blob,)
> >>> tple
> ('1\n2\n3\n4\n',)
> >>> values = [int(f) for f in tple[0].split()]
> >>> values
> [1, 2, 3, 4]
>
> --
>         Wulfraed                 Dennis Lee Bieber         AF6VN
>         wlfr...(a)ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Perfect! Thanks for your help. When I tried to do what you did, I
had explictly tried to for it as an array by using the array keyword
in from of the loop. Everything works, and now I can move on to my
next problem.

Ian
From: John Nagle on
Ian Hoffman wrote:
> Hello,
>
> I'm having significant Python difficulties (and I'm new to Python).
> I'm trying to read BLOB ASCII (numerical) data from a MySQL database
> using MySQLdb in a formatted fashion. The BLOB data is a sequence of
> numbers separated by newlines (\n), like this:
> 5
> 6
> 10
> 45
> etc.

Note that a BLOB is not ASCII. If you're storing ASCII text, use type
TEXT in SQL, not type BLOB. Don't lie to the database. It doesn't like that.
And if you're going to store numbers, store numbers, not text. SQL has
the usual integer and floating point types.

When you read a BLOB from MySQLdb, you do not get a string. You get
an object of type "bytes". This is not a Python string. Python strings
can be ASCII or Unicode in Python 2.x, and in 3.x, are always Unicode.

John Nagle