MySQL, Python, NumPy and formatted read

I

Ian Hoffman

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?
 
M

Matteo Landi

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.
 
I

Ian Hoffman

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


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.



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?

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
 
I

Ian Hoffman

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]

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
 
J

John Nagle

Ian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top