csv模块奇怪。 [英] csv module strangeness.

查看:76
本文介绍了csv模块奇怪。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义方言创建一个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


-

通过 http://www.teranews.com

解决方案

好的,我是个白痴。我甚至没有把我的方言

对象传给读者()电话。


所以现在它起作用了,但它仍然很奇怪/>
缺席默认值。


Tobiah


这样运行,但分隔符是仍然是逗号。

当list.csv是逗号分隔时,它可以正常工作,

但是当list.csv有制表符分隔值时,我

回到一个包含整行的单个字段。


我想我一定是做了可怕的错误。


谢谢,


Tobiah



-

通过 http://www.teranews.com


在< 44 ********************** @ free.teranews.com> ;, tobiah写道:


我正在尝试使用自定义di创建一个cvs.reader对象alect。


文档有点简洁,但我认为我应该将bvs.Dialect子类化为



类方言(csv.Dialect):

通过


现在,文档说所有属性都合理

默认值,但实例化以上内容给出:


回溯(最近一次调用最后一次):

文件"< stdin>",line 15,在?

文件" /usr/local/lib/python2.4/csv.py" ;,第39行,在__init__

引发错误,方言没有验证:%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

dialect.quoting = csv.QUOTE_MINIMAL



这是可能的,但为什么你不遵循这条路? csv.Dialect`设置了

类属性?


class MyDialect(csv.Dialect):

delimiter =''\\ \\ t''

lineterminator =''\ n''

#等等?|


Ciao,

Marc''BlackJack''Rintsch



>

那是可能的,但是为什么你不遵循`csv.Dialect`设置

类属性的方式?


class MyDialect(csv .Dialect):

分隔符=''\t''

lineterminator =''\ n''

#等等ona ?|



因为我被困了。


-

通过免费发布来自 http://www.teranews.com 的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

解决方案

Ok, I''m an idiot. I didn''t even pass my dialect
object to the reader() call.

So now it works, but it is still strange about
the absent defaults.

Tobiah

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


In <44**********************@free.teranews.com>, tobiah wrote:

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

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


>
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?|

Because I''m hung over.

--
Posted via a free Usenet account from http://www.teranews.com


这篇关于csv模块奇怪。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆