0

I have a log file with data inside.

I would like to convert this file into a CSV (Excel) file.

I use Eclipse and write in Jython (all latest versions) but when I try to import CSV files I always get this error:

ImportError: no module named csv.

Do you know why?

This is my program:

import csv
r = open('file.log') 
w = open('newfile.csv','w') 
writer = csv.writer(w)
for row in r.readlines():
    writer.writerow(row.split())
r.close() 
w.close()

Now I tried openCSV. The CSV file is created, but it is empty.

In fact, the problem comse from the writeAll.

If I put writeNext, only one line appears in the CSV file (that's normal), but with writeAll, the file is empty.

Do you know how I can resolve my problems?

This is my program:

from au.com.bytecode.opencsv import *
from java.io import *
for line in open("out.log"):
    try :   
        en = line.split(" ")
        writer = CSVWriter(FileWriter("out.csv"))
        writer.writeAll(en)
    except : IOException
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
tranen
  • 25
  • 5

2 Answers2

2

Jython doesn't seem to have a csv module (it's odd, though, it's documented in some places). Instead, you should use a Java library to accomplish the same. It seems that people like OpenCSV, but you can decide for yourself (there's a question about good Java CSV libraries on SO).

I can't weigh in with certainty as to whether or not this elusive CSV module actually exists. However, you can always use an existing Java library.

Community
  • 1
  • 1
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 3
    Why is `csv` listed in the [Jython library documentation](http://www.jython.org/docs/library/csv.html)? That's quite confusing! – mgiuca Feb 01 '11 at 03:30
  • @mgiuca that's odd, because it seems like it's documented in one place and not the other. I couldn't find anything like it in the Jython source, so I think it's safe to assume that it either doesn't exist or isn't in perfect working order. Could be wrong though. – Rafe Kettler Feb 01 '11 at 03:36
  • Thanks I'll go to the see the OpenCSV. – tranen Feb 01 '11 at 04:00
1

It looks like csv is implemented in Jython 2.5.3. I tried the examples from Python Module of the Week and they work.

benevolentprof
  • 1,507
  • 2
  • 13
  • 24