Parsing a file through a Python script
I am trying to parse a file with a Python script (the file is stored in the same folder as the script) : "marco" is the id of the file : data=str(container.marco) i=0 for line in data.readlines(): ..... I get a readlines : attribute error Could anyone help me? Sincerely, Pierre Godefroy
Pierre Godefroy wrote:
I am trying to parse a file with a Python script (the file is stored in the same folder as the script) : "marco" is the id of the file :
data=str(container.marco) i=0 for line in data.readlines(): .....
You might want to try something like from StringIO import StringIO data = StringIO(str(getattr(container,'marco').data)) Bories -- Bories v. dem Bussche Duesseldorf - Germany
Thanks, but it does not work as it says I am unauthorized in this context (valid only only in an external script). However I uploaded the file through a form and had no problem (readlines() passed through without any warning) : file = REQUEST.form['file_attachment'] for line in file.readlines(): .... Which is kind of strange. The issue is, I would like to work on a very big file and there may be problems with this. I would like to to access the file as it is already stored in the ZODB. Any other idea? Pierre Godefroy Bories v. dem Bussche wrote:
Pierre Godefroy wrote:
I am trying to parse a file with a Python script (the file is stored in the same folder as the script) : "marco" is the id of the file :
data=str(container.marco) i=0 for line in data.readlines(): .....
You might want to try something like
from StringIO import StringIO data = StringIO(str(getattr(container,'marco').data))
Bories
Pierre Godefroy wrote:
Thanks, but it does not work as it says I am unauthorized in this context (valid only only in an external script).
which is strange, since something like that runs here not in an "External Script", but in a normal container Python Script Bories -- Bories v. dem Bussche Duesseldorf - Germany
On Sun, 2003-11-30 at 07:25, Pierre Godefroy wrote:
The issue is, I would like to work on a very big file and there may be problems with this. I would like to to access the file as it is already stored in the ZODB.
Well... 1. Don't serve large stuff from ZODB if you can avoid it. 2. If you want to parse *any* ZODB content, do this: --- data = getattr(context, my_obj_name) for line in data.readlines(): # do stuff --- HTH, Dylan
Pierre Godefroy wrote:
I am trying to parse a file with a Python script (the file is stored in the same folder as the script) : "marco" is the id of the file :
data=str(container.marco) i=0 for line in data.readlines(): .....
I get a readlines : attribute error
Could anyone help me?
Python scripts are very limited (by design) regarding available functions and modules. Check the Zope book 2.6 (sorry don't remember which section). The recomended solution is to use External methods (if you have access to the zope instalation). Quite simple: create a .py file with your functions in the folder External and the link to the function you want from the zope ZMI. Then you can use this (external) function as any other zope available function. Regards, Fernando
Pierre, The file "marco" is, presumably, a Zope ZOOB file. Your variable data is a string containing that files content. You can split it into lines using lines = data.split('\n') and then iterate through the lines. On Sun, 30 Nov 2003, Fernando Martins wrote:
Pierre Godefroy wrote:
I am trying to parse a file with a Python script (the file is stored in the same folder as the script) : "marco" is the id of the file :
data=str(container.marco) i=0 for line in data.readlines(): .....
I get a readlines : attribute error
Could anyone help me?
Python scripts are very limited (by design) regarding available functions and modules. Check the Zope book 2.6 (sorry don't remember which section).
The recomended solution is to use External methods (if you have access to the zope instalation). Quite simple: create a .py file with your functions in the folder External and the link to the function you want from the zope ZMI. Then you can use this (external) function as any other zope available function.
Regards, Fernando
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
participants (5)
-
Bories v. dem Bussche -
Dennis Allison -
Dylan Reinhardt -
Fernando Martins -
Pierre Godefroy