RE: [Zope] RE: CSV (Was: Perl scripts)
So back to the original topic: Is there any example how to use an external Perl script and shipping some parameters to it? I just found the statement that it is possible. I wonder if anybody tried ...
I regularly use a Perl script called from Zope to do the opposite conversion: *from* xls *to* csv. Initially, I used this code in an external python script, but I've now created a Zope product called 'CSVFile' which checks the file extension and automatically converts uploaded files from xls to csv if required. Here is the appropriate code snippet: ## ## Configuration ## ## Perl Path PERL_PATH='/usr/local/bin/perl' ## Location of perl script 'xls2csv.pl' XLS2CSV_PATH = '/usr/local/zope/current/Extensions/xls2csv.pl' # ... other stuff here ... def ExcelToCSV(ExcelData=None,ExcelName='ExcelFile.xls',CSVName='CSVFile.csv'): """Translate the first worksheet of a Microsoft Excel file to CSV format by calling the perl script 'xls2csv.pl' """ if ExcelData == None: return None dirinfo = do_tmpdir() excelfile = open(ExcelName,"w") excelfile.write(ExcelData) excelfile.close() HERE = dirinfo['tmpdir'] command = "%s %s %s %s 1 2>1 > tmp.out " % (PERL_PATH, XLS2CSV_PATH, HERE + ExcelName, HERE + CSVName ) # print "Executing command '%s'" % command retval = os.system( command ) if retval==0: csvfile = open(CSVName,"r") CSVData = csvfile.read() csvfile.close() else: CSVData = None clean_tmpdir(dirinfo) return CSVData As an aside, I use double-quotes to bracket each cells contents and comma separation. With this, I haven't had any problems here with MS-Excel failing to properly convert csv files when opening them -- with any of IE, Mozilla, Netscape 4 on W2K. -Greg LEGAL NOTICE Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
On Fri, 18 Oct 2002, Warnes, Gregory R wrote:
I regularly use a Perl script called from Zope to do the opposite conversion: *from* xls *to* csv. Initially, I used this code in an external python script, This is the way I decided to go now: Use an External Python Script to call a Perl Script in local file system. I've thought I misunderstood something in the documentation that I could Perl directly in an External Script without wrapping it in Python. (Perhaps the hint of Andy McKay will enlighten me once www.zope.org is available again ...)
but I've now created a Zope product called 'CSVFile' which checks the file extension and automatically converts uploaded files from xls to csv if required. Here is the appropriate code snippet: Thanks. I'll use this as an example.
As an aside, I use double-quotes to bracket each cells contents and comma separation. With this, I haven't had any problems here with MS-Excel failing to properly convert csv files when opening them -- with any of IE, Mozilla, Netscape 4 on W2K. I'll give this a further try. Did you also testet it under Linux with Gnumeric?
Kind regards Andreas.
Hi, --On Montag, 21. Oktober 2002 10:11 +0200 Andreas Tille <tillea@rki.de> wrote:
On Fri, 18 Oct 2002, Warnes, Gregory R wrote:
I regularly use a Perl script called from Zope to do the opposite conversion: *from* xls *to* csv. Initially, I used this code in an external python script, This is the way I decided to go now: Use an External Python Script to call a Perl Script in local file system. I've thought I misunderstood something in the documentation that I could Perl directly in an External Script without wrapping it in Python. (Perhaps the hint of Andy McKay will enlighten me once www.zope.org is available again ...)
Still, if you want to _create_ csv from database, you sure dont need perl. Remember the overhead of calling a foreign interpreter just for generation of a text string which could be done in python easyly, without even requiring external methods. You only have to analyze the output of your perl script and make your python method output the same. This schould not be so hard. Regards Tino
On Mon, 21 Oct 2002, Tino Wildenhain wrote:
Still, if you want to _create_ csv from database, you sure dont need perl. Remember the overhead of calling a foreign interpreter just for generation of a text string which could be done in python easyly, without even requiring external methods. You only have to analyze the output of your perl script and make your python method output the same. This schould not be so hard. For sure I will not use Perl for creating CSV. This would be *really* stupid. On the other hand I observed that creating the CSV from larger data sets was *very* slow (unacceptable long download time :-(). So I have to do something here ...
Kind regards Andreas.
Hi Andreas, --On Montag, 21. Oktober 2002 11:34 +0200 Andreas Tille <tillea@rki.de> wrote:
On Mon, 21 Oct 2002, Tino Wildenhain wrote:
Still, if you want to _create_ csv from database, you sure dont need perl. Remember the overhead of calling a foreign interpreter just for generation of a text string which could be done in python easyly, without even requiring external methods. You only have to analyze the output of your perl script and make your python method output the same. This schould not be so hard. For sure I will not use Perl for creating CSV. This would be *really* stupid. On the other hand I observed that creating the CSV from larger data sets was *very* slow (unacceptable long download time :-(). So I have to do something here ...
Ah yes, we do similar here - creating an average big csv file from shop system (prices, descriptions etc.) for 15000 lines. We do it by starting with a query which is limited to 100 lines, sending the stuff via request.write() then do the next query with offset 100, limit 200, then sending, then next query with offset 300, limit 400 and so on. You get the picture. This way the transfer starts almost immediately and looks like it is continous to the user.
Kind regards
Andreas.
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
On Fri, 18 Oct 2002, Warnes, Gregory R wrote:
## ## Configuration ##
## Perl Path PERL_PATH='/usr/local/bin/perl'
## Location of perl script 'xls2csv.pl' XLS2CSV_PATH = '/usr/local/zope/current/Extensions/xls2csv.pl'
# ... other stuff here ...
def ExcelToCSV(ExcelData=None,ExcelName='ExcelFile.xls',CSVName='CSVFile.csv'): """Translate the first worksheet of a Microsoft Excel file to CSV format by calling the perl script 'xls2csv.pl' """ ... if retval==0: csvfile = open(CSVName,"r") CSVData = csvfile.read() csvfile.close() else: CSVData = None
clean_tmpdir(dirinfo)
return CSVData
This works like a charm and I'm very happy with this solution. The only thing I would like to do now is the equivalent of response.setHeader('Content-type', 'application/vnd.ms-excel') response.setHeader('Content-Length', len(result_str)) response.setHeader('Content-Disposition','attachment;filename=filename.csv') which I did in a Python Script. In short - how to reference the response opject inside an External Method. Kind regards Andreas.
participants (3)
-
Andreas Tille -
Tino Wildenhain -
Warnes, Gregory R