From: Updike, C. <Cla...@jh...> - 2004-02-26 15:22:24
|
Doh! I forgot what the 'c' in csv stood for (I am always working with tab delimited formats sent from Excel via http). In case it's not obvious, just replace the "\t" below with a "," and it will work for csv. -Clark -----Original Message----- From: Updike, Clark Sent: Thursday, February 26, 2004 10:02 AM Subject: RE: [Jython-users] Importing csv.pyd in Jython I do a lot of work with csv formats and have always just used plain old code to do it. But here are some helper functions you can easily customize and use with files: >>> def list2csv(l): ... return "\n".join(["\t".join([str(cell) for cell in row]) for row in l]) ... >>> def csv2list(csvString): ... return [row.split("\t") for row in csvString.split("\n")] ... >>> l = [[1,2,3],[4,5,6]] >>> print list2csv(l) 1 2 3 4 5 6 >>> print csv2list(list2csv(l)) [['1', '2', '3'], ['4', '5', '6']] >>> l = [['a','b','c'],['d','e','f']] >>> csv2list(list2csv(l)) == l 1 list2csv is converting everything to strings which may be nugatory if you're only working with lists of strings. If list comprehension one-liners are bothersome to you, you can always unroll them into for loops. -Clark -----Original Message----- From: Chris Sent: Thursday, February 26, 2004 7:38 AM To: jyt...@li... Subject: [Jython-users] Importing csv.pyd in Jython Hi I wish to read csv files from within Jython. Unfortunately, there is a csv module in Python 2.3 which Jython 2.1 does support. Rock on Jython 2.2! Meanwhile, I found a compiled csv python module from <http://www.object-craft.com.au/projects/csv>. A precompiled csv.pyd file is available for download for Python 2.1 and for Python 2.2. I have tried importing it in my Jython file, however, Jython does not appear to recognise it. Any ideas? |