CSV模块,DictReader问题(bug?) [英] CSV module, DictReader problem (bug?)

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

问题描述

自从我编写Python代码已经一年左右了,所以也许

我只是在做一些非常愚蠢的事情,但是...


文档

=============

类DictReader(csvfile [,fieldnames = None,

[,restkey =无[,restval =无[,dialect =''excel''

[,* args,** kwds]]]]])

创建一个像普通读者一样操作的对象

但是将读取的信息映射到一个字典中,其键为

由可选的fieldnames参数给出。如果省略

fieldnames参数,则csvfile的第一行

中的值将用作字段名。


代码

====


导入csv


r = csv.DictReader(''C:\\ \\ temp\Book1.csv'')

打印r.next()

#EOF


输出

======


{''C'':'':''}

It''s been a year or so since I written Python code, so maybe
I am just doing something really dumb, but...

Documentation
=============

class DictReader(csvfile[,fieldnames=None,
[,restkey=None[, restval=None[, dialect=''excel''
[, *args, **kwds]]]]])
Create an object which operates like a regular reader
but maps the information read into a dict whose keys
are given by the optional fieldnames parameter. If the
fieldnames parameter is omitted, the values in the
first row of the csvfile will be used as the fieldnames.

Code
====

import csv

r = csv.DictReader(''C:\Temp\Book1.csv'')
print r.next()
# EOF

Output
======

{''C'': '':''}

推荐答案

Jeff Blaine写道:
Jeff Blaine wrote:

自从我编写Python代码以来已经有一年左右了,所以也许

我只是在做一些非常愚蠢的事情,但是......


文档

=============


类DictReader(csvfile [,fieldnames =无,

[,restkey =无[,restval =无[,方言=''excel''

[,* args,** kwds]]]]])


创建一个像普通读者一样操作的对象

bu t将读取的信息映射到一个字典,其键为

由可选的fieldnames参数给出。如果省略

fieldnames参数,则csvfile的第一行

中的值将用作字段名。


代码

====


导入csv


r = csv.DictReader(''C:\\ \\ Temp\Book1.csv'')

打印r.next()

#EOF
It''s been a year or so since I written Python code, so maybe
I am just doing something really dumb, but...

Documentation
=============

class DictReader(csvfile[,fieldnames=None,
[,restkey=None[, restval=None[, dialect=''excel''
[, *args, **kwds]]]]])
Create an object which operates like a regular reader
but maps the information read into a dict whose keys
are given by the optional fieldnames parameter. If the
fieldnames parameter is omitted, the values in the
first row of the csvfile will be used as the fieldnames.

Code
====

import csv

r = csv.DictReader(''C:\Temp\Book1.csv'')
print r.next()
# EOF



这里是常规阅读器的文档,来自Python 2.5:


阅读器(...)

csv_reader = reader(iterable [,dialect = ''excel'']

[可选关键字args])

for csv_reader中的行:

process(row)


可迭代参数可以是任何返回每行迭代的输入行的对象,例如文件对象或列表。

...

所以读者只是循环遍历文件名中的字符。尝试


r = csv.DictReader(打开(''C:\Temp \Book1.csv''))


代替。


< / F>

here''s the documentation for the regular reader, from Python 2.5:

reader(...)
csv_reader = reader(iterable [, dialect=''excel'']
[optional keyword args])
for row in csv_reader:
process(row)

The "iterable" argument can be any object that returns a line
of input for each iteration, such as a file object or a list.
...

so the reader is simply looping over the characters in your filename. try

r = csv.DictReader(open(''C:\Temp\Book1.csv''))

instead.

</F>


我看到了什么是错的。我。哇我生锈了。


Jeff Blaine写道:
I see what''s wrong. Me. Wow am I ever rusty.

Jeff Blaine wrote:

自从我写完以来已经有一年左右了Python代码,所以也许

我只是在做一些非常愚蠢的事情,但是...


文档

==== =========

class DictReader(csvfile [,fieldnames = None,

[,restkey = None [,restval = None [ ,dialect =''excel''

[,* args,** kwds]]]]])


创建一个像常规一样操作的对象读者

但将读取的信息映射到一个字典中,其键为

由可选的fieldnames参数给出。如果省略

fieldnames参数,则csvfile的第一行

中的值将用作字段名。


代码

====


导入csv


r = csv.DictReader(''C:\\ \\ temp\Book1.csv'')

打印r.next()

#EOF


输出

======


{''C'':'':''}
It''s been a year or so since I written Python code, so maybe
I am just doing something really dumb, but...

Documentation
=============

class DictReader(csvfile[,fieldnames=None,
[,restkey=None[, restval=None[, dialect=''excel''
[, *args, **kwds]]]]])
Create an object which operates like a regular reader
but maps the information read into a dict whose keys
are given by the optional fieldnames parameter. If the
fieldnames parameter is omitted, the values in the
first row of the csvfile will be used as the fieldnames.

Code
====

import csv

r = csv.DictReader(''C:\Temp\Book1.csv'')
print r.next()
# EOF

Output
======

{''C'': '':''}


Jeff Blaine写道:
Jeff Blaine wrote:

自从我编写Python代码以来已经有一年左右了,所以也许

我只是在做一些非常愚蠢的事情,但是...


文档

=============


class DictReader(csvfile [,fieldnames = None,

[,restkey = None [,restval = None [,dialect =''excel''

[,* args,** kwds]]]]])


创建一个像普通读者一样操作的对象

但是将读取的信息映射到一个字典,其键为

由可选的fieldnames参数给出。如果省略

fieldnames参数,则csvfile的第一行

中的值将用作字段名。


代码

====


导入csv


r = csv.DictReader(''C:\\ \\ Temp\Book1.csv'')
It''s been a year or so since I written Python code, so maybe
I am just doing something really dumb, but...

Documentation
=============

class DictReader(csvfile[,fieldnames=None,
[,restkey=None[, restval=None[, dialect=''excel''
[, *args, **kwds]]]]])
Create an object which operates like a regular reader
but maps the information read into a dict whose keys
are given by the optional fieldnames parameter. If the
fieldnames parameter is omitted, the values in the
first row of the csvfile will be used as the fieldnames.

Code
====

import csv

r = csv.DictReader(''C:\Temp\Book1.csv'')



问题1:


""" csvfile可以是任何支持迭代器协议的对象和

每次调用下一个方法时都会返回一个字符串 - 文件对象

和列表对象都是合适的。如果csvfile是一个文件对象,那么它必须在平台上用b标志打开,并且它会产生

的差异。""


因此,打开文件,以便next()方法返回下一个

内容块,而不是文件名中的下一个字节。


请注意,arg被称为csvfile,而不是csvfilename。


问题2:[在这个例子中确定,但这就像是说你已经采取了

在雷区一步并且还活着]反斜杠和Windows

文件名:


如果您要写''c:\ temp \ book1.csv'',它会爆炸...因为

\t -tab和\b - 退格。习惯*总是*使用原始的

字符串r''C:\ Temp \ Book1.csv''用于Windows文件名(和重新模式)。

你可以使用双反斜杠''C:\\Temp \\Book1.csv'',但它是

uglier。

Problem 1:

"""csvfile can be any object which supports the iterator protocol and
returns a string each time its next method is called -- file objects
and list objects are both suitable. If csvfile is a file object, it
must be opened with the ''b'' flag on platforms where that makes a
difference."""

So, open the file, so that the next() method returns the next chunk of
content, not the next byte in the name of the file.

Note that the arg is called "csvfile", not "csvfilename".

Problem 2: [OK in this instance, but that''s like saying you have taken
one step in a minefield and are still alive] backslashes and Windows
file names:

If you were to write ''c:\temp\book1.csv'', it would blow up ... because
\t -tab and \b -backspace. Get into the habit of *always* using raw
strings r''C:\Temp\Book1.csv'' for Windows file names (and re patterns).
You could use double backslashing ''C:\\Temp\\Book1.csv'' but it''s
uglier.


打印r.next()

#EOF


输出

======


{''C'':'':''}
print r.next()
# EOF

Output
======

{''C'': '':''}



HTH,

约翰

HTH,
John


这篇关于CSV模块,DictReader问题(bug?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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