问题描述
我正在尝试使用自定义方言创建一个cvs.reader对象。
文档有点简洁,但我认为我应该这样做
继承cvs.Dialect:
类方言(csv.Dialect):
通过
现在,文档说所有属性都有合理的
默认值,但实例化上面给出了:
Traceback(最近一次调用最后一次):
文件"< stdin>",第15行,在?
文件" /usr/local/lib/python2.4/csv.py" ;, line 39,在__init__
引发错误,Dialect没有验证:%s %"," .join(错误)
_csv.Error:Dialect没有验证:未设置分隔符,未设置quotechar,未设置lineterminator,doublequote参数必须为True或False,skipinitialspace参数必须为True或False,引用参数未设置
所以我看一下源代码。 Dialect课很简单,
并且开头:
class方言:
_name =""
_valid = False
#占位符
分隔符=无
quotechar =无
escapechar =无
doublequote =无
skipinitialspace =无
lineterminator =无
引用=无
所以,难怪它的validate()调用失败了。
我唯一想到的就是设置
这些在实例化之前就在类本身上:
########################## #####################
import csv
class dialect(csv.Dialect) :
通过
dialect.delimiter =" \t"
dialect.quotechar =''"''
dialect.lineterminator =" \ n"
dialect.doublequote = True
dialect.skipinitialspace = True
方言。 quoting = csv.QUOTE_MINIMAL
d = dialect()
reader = csv.reader(open(''list.csv''))
用于读取行:
打印行
################### ############################
这样运行,但分隔符仍然是逗号。
当list.csv是逗号分隔时,它可以正常工作,
但是当list.csv有制表符分隔值时,我
回来一个单一的字段,整个行在
它。
我想我一定是做了一些可怕的错误。
谢谢,
Tobiah
-
通过
-
通过
这是可能的,但为什么你不遵循这条路? csv.Dialect`设置了
类属性?
class MyDialect(csv.Dialect):
delimiter =''\\ \\ t''
lineterminator =''\ n''
#等等?|
Ciao,
Marc''BlackJack''Rintsch
因为我被困了。
-
通过免费发布来自的Usenet帐户
I''m trying to create a cvs.reader object using a custom dialect.
The docs are a little terse, but I gather that I am supposed
to subclass cvs.Dialect:
class dialect(csv.Dialect):
pass
Now, the docs say that all of the attributes have reasonable
defaults, but instantiating the above gives:
Traceback (most recent call last):
File "<stdin>", line 15, in ?
File "/usr/local/lib/python2.4/csv.py", line 39, in __init__
raise Error, "Dialect did not validate: %s" % ", ".join(errors)
_csv.Error: Dialect did not validate: delimiter character not set, quotechar not set, lineterminator not set, doublequote parameter must be True or False, skipinitialspace parameter must be True or False, quoting parameter not set
So I look at the source. The Dialect class is very simple,
and starts with:
class Dialect:
_name = ""
_valid = False
# placeholders
delimiter = None
quotechar = None
escapechar = None
doublequote = None
skipinitialspace = None
lineterminator = None
quoting = None
So, it''s no wonder that it fails its validate() call.
The only thing that I can think of to do is to set
these on the class itself before instantiation:
###############################################
import csv
class dialect(csv.Dialect):
pass
dialect.delimiter = "\t"
dialect.quotechar = ''"''
dialect.lineterminator = "\n"
dialect.doublequote = True
dialect.skipinitialspace = True
dialect.quoting = csv.QUOTE_MINIMAL
d = dialect()
reader = csv.reader(open(''list.csv''))
for row in reader:
print row
###############################################
This runs, but the delimiter is still the comma.
When list.csv is comma delim, it works correctly,
but when list.csv has tab separated values, I
get back a single field with the entire line in
it.
I suppose I must be doing something horribly wrong.
Thanks,
Tobiah
--
Posted via a free Usenet account from http://www.teranews.com
--
Posted via a free Usenet account from http://www.teranews.com
That''s possible but why didn''t you follow the way `csv.Dialect` set the
class attributes?
class MyDialect(csv.Dialect):
delimiter = ''\t''
lineterminator = ''\n''
# and so ona?|
Ciao,
Marc ''BlackJack'' Rintsch
Because I''m hung over.
--
Posted via a free Usenet account from http://www.teranews.com
这篇关于csv模块奇怪。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!