trying to open a file in database but couldn't
I'm trying to open a file that is in database but I got this error message instead coercing to Unicode: need string or buffer, instance found I don't quite understand why. My code looks like this def imageSetup(self, dataID, REQUEST): from StringIO import StringIO # Get the original image and data in memory and open the file imageData=getattr(self, dataID) imageDataFile=StringIO(str(imageData.data)) tfw = open(imageDataFile, "r") I upload a text file into ZOPE database and the file consist of six lines of float point numbers that I want to use with REQUEST.set I call this method using < dtml-call "imageSetup(textFile, REQUEST)" > can anyone help me... please... did I made a mistake with this code --------------------------------- Yahoo! Mail Bring photos to life! New PhotoMail makes sharing a breeze.
--On 25. Februar 2006 21:55:24 -0800 Allen Huang <swapp0@yahoo.com> wrote:
imageDataFile=StringIO(str(imageData.data)) tfw = open(imageDataFile, "r")
Please consult the StringIO documentation. There is no need to open() a StringIO instance...also your code makes little sense to me because str() already returns a string which is usually what you need and want. Why do you have the need to put the image data into a StringIO instance. And another point: you should always use cStringIO instead of StringIO (for performance reasons)...consult the Python Library reference for details. -aj
Hi.. thanks for replying and thanks for the help I didn't realize that I mis type the code. The code what suppose to be .. imageDataFile=StringIO(str(dataID.data)) tfw = open(imageDataFile, "r") so, what your saying is that I don't need to open the file that I wanted to read into and extract the data? But how can that be? I originally used tried by extracting info from a file on the computer and it works. But I couldn't do the same thing from the web.. I don't get that I did wrong or missing... How would you write the external method if you were to read a text file that you store in ZOPE database and extract the infromation line by line? Andreas Jung <lists@andreas-jung.com> wrote: --On 25. Februar 2006 21:55:24 -0800 Allen Huang wrote:
imageDataFile=StringIO(str(imageData.data)) tfw = open(imageDataFile, "r")
Please consult the StringIO documentation. There is no need to open() a StringIO instance...also your code makes little sense to me because str() already returns a string which is usually what you need and want. Why do you have the need to put the image data into a StringIO instance. And another point: you should always use cStringIO instead of StringIO (for performance reasons)...consult the Python Library reference for details. -aj --------------------------------- Yahoo! Mail Use Photomail to share photos without annoying attachments.
On 26.02.06 03:52:14, Allen Huang wrote:
I didn't realize that I mis type the code. The code what suppose to be .. imageDataFile=StringIO(str(dataID.data))
If dataId is a string this won't work.
tfw = open(imageDataFile, "r")
so, what your saying is that I don't need to open the file that I wanted to read into and extract the data? But how can that be?
You have to forget about files when you're working with ZODB, because this Database doesn't have any file in it. Those are all objects and when you do something like myobj = getattr(self, 'name') you'll get an object. This object might be an instance of the File class and thus behave like a file, but it still is only an object. Your StringIO constructor already get the content of this object, i.e. the text that is stored in it, because you give it via myobj.data. The next thing is, if you already can access the text content via myobj.data you don't need StringIO.
How would you write the external method if you were to read a text file that you store in ZOPE database and extract the infromation line by line?
Suppose dataID contains the id of the object something like this: obj = getattr(self, dataID) for line in str(obj.data).split('\n'): dosth_with(line) That's all. Andreas -- You will gain money by an immoral action.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Andreas Jung wrote:
--On 25. Februar 2006 21:55:24 -0800 Allen Huang <swapp0@yahoo.com> wrote:
imageDataFile=StringIO(str(imageData.data)) tfw = open(imageDataFile, "r")
Please consult the StringIO documentation. There is no need to open() a StringIO instance...also your code makes little sense to me because str() already returns a string which is usually what you need and want. Why do you have the need to put the image data into a StringIO instance. And another point: you should always use cStringIO instead of StringIO (for performance reasons)...consult the Python Library reference for details. -aj
In cases where it matters, cStringIO is not Unicode-safe: http://www.python.org/doc/current/lib/module-cStringIO.html Tres. - -- =================================================================== Tres Seaver +1 202-558-7113 tseaver@palladion.com Palladion Software "Excellence by Design" http://palladion.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFEAf6J+gerLs4ltQ4RAuqQAKDD81PwP9o/0kWMyvqOd3MuexIFzgCfduWS PpS/bwV5AruJwA6pHZFsRA8= =09y+ -----END PGP SIGNATURE-----
participants (4)
-
Allen Huang -
Andreas Jung -
Andreas Pakulat -
Tres Seaver