parsing CSV file in my jython script

559 views
Skip to first unread message

NItin

unread,
Jul 24, 2009, 3:45:54 AM7/24/09
to Marathon Java GUI Testing
Hi all

i want to read csv file in my jython script. for that i could not see
any api currently available in jython. Or is there?
There is jython's csv module , but it is not supported in
marathon-2.0b4 version.

so i am trying to use opencsv-1.8 external library.

i added this jar in to my class path, and i have code as shown below:

#{{{ Marathon Fixture
from default import *
from au.com.bytecode.opencsv import CsvReader
#}}} Marathon Fixture

def test():
java_recorded_version = '1.6.0_06'

if window('Test Script Editor '):
reader = CsvReader("patient.csv")
close()

I m getting ImportError: cannot import name CsvReader

I have question:

how to go about csv file parsing in jython?


Cearum

unread,
Jul 30, 2009, 2:55:57 PM7/30/09
to Marathon Java GUI Testing
I found this method for converting a csv to a list somewhere. I can't
take credit for it, I don't remember where I got it, but it helped me
out a ton.

def csv2list(csvString):
return [row.split(",") for row in csvString.split("\n")]

That should turn something like this:

asdf, asdf, asdf, asdf,\n
asdf, asdf, asdf, asdf,\n

into this:

[ ['asdf', 'asdf', 'asdf', 'asdf']
['asdf', 'asdf', 'asdf', 'asdf'] ]


To bring in the csv file I just use this:

f = open("whateverfile.csv", "rb")
lines = f.read()
f.close()
lines = csv2list(lines)

something to remember is if your other separated text has commas in
it, it will have troubles. In that case you could use an escaped comma
like so:

def csv2list(csvString):
return [row.split("\,") for row in csvString.split("\n")]

Hopefully this is a good starting point.

On Jul 24, 3:45 am, NItin <nitipa...@gmail.com> wrote:
> Hiall
Reply all
Reply to author
Forward
0 new messages