创建 CSV 文件
我有一个包含数据的日志文件。
我想将此文件转换为 CSV (Excel) 文件。
我使用 Eclipse 并在 Jython(所有最新版本)中编写,但是当我尝试导入 CSV 文件时,我总是收到此错误:
导入错误:没有名为 csv 的模块。
你知道为什么吗?
这是我的程序:
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()
现在我尝试了 openCSV。 CSV 文件已创建,但它是空的。
事实上,问题出在writeAll
上。
如果我输入 writeNext
,CSV 文件中只会出现一行(这是正常的),但使用 writeAll
时,文件为空。
你知道我该如何解决我的问题吗?
这是我的程序:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Jython 似乎没有
csv
模块(不过这很奇怪,它在某些地方有记录)。相反,您应该使用 Java 库来完成相同的任务。似乎人们喜欢 OpenCSV,但你可以自己决定(有一个关于好的 Java CSV 库的问题) 。我无法确定这个难以捉摸的 CSV 模块是否确实存在。但是,您始终可以使用现有的 Java 库。
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.
看起来 csv 是在 Jython 2.5.3 中实现的。我尝试了 本周 Python 模块 中的示例,它们有效。
It looks like csv is implemented in Jython 2.5.3. I tried the examples from Python Module of the Week and they work.