[Zope] Perl scripts

Charlie Reiman creiman@kefta.com
Wed, 9 Oct 2002 16:17:11 -0700


> -----Original Message-----
> From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Chris
> Withers
> Sent: Wednesday, October 09, 2002 3:49 PM
> To: Andreas Tille
> Cc: Zope user list
> Subject: Re: [Zope] Perl scripts
>
>
> Andreas Tille wrote:
> > Hello,
> >
> > The Zope Book mentions that you can use Perl with Zope - but that's all
> > I've found about Perl usage.  I have to convert some database query
> > to an Excel file and the only way I know to do this is the
> >
> >          Spreadsheet::WriteExcel
>
> Why not just write the results out as a .csv?
>
> You can open them in Excel...
>
> cheers,
>
> Chris

Yes, this is pretty easy. Some python script to get you going:
---------------
rs = some_sql_query()

for item in rs:
  result_str += (str(item.DATE) + ',' +
                 str(item.STUFF) + ',' +
                 str(item.MORESTUFF) + '\n')

response.setHeader('Content-type', 'application/vnd.ms-excel')
response.setHeader('Content-Length', len(result_str))
response.setHeader('Content-Disposition',
'attachment;filename=filename.csv')
return result_str
----------------

Be warned that mozilla doesn't handle this quite right. It launches excel
and hands it the data but seems to translate the EOL characters in the
process so excel refuses to split the columns. Ironically, Excel 2000 only
accepts the unix EOL character for csv data. So: it works fine from IE but
not so fine from Mozilla. If anyone has a dual browser solution I'd like to
see it.

I'm also leaving out the necessary code to escape quotes and quote commas
(because I can't seem to find it right now).