unicodecsv.DictReader如何表示一个csv文件 [英] How does unicodecsv.DictReader represent a csv file

查看:72
本文介绍了unicodecsv.DictReader如何表示一个csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习有关python中数据分析的Udacity课程,并且我们一直在使用unicodecsv库。

I'm currently going through the Udacity course on data analysis in python, and we've been using the unicodecsv library.

更具体地说,我们编写了以下代码,该代码读取一个csv文件并将其转换为列表。这是代码:

More specifically we've written the following code which reads a csv file and converts it into a list. Here is the code:

def read_csv(filename):
with open(filename,'rb')as f:
    reader = unicodecsv.DictReader(f)
    return list(reader) 

为了解决这个问题,我试图弄清楚字典和列表中数据的表示方式,我感到非常困惑。有人可以向我解释一下。

In order to get my head around this, I'm trying to figure out how the data is represented in the dictionary and the list, and I'm very confused. Can someone please explain it to me.

例如,我不明白的一件事是为什么以下内容会引发错误

For example, one thing I don't understand is why the following throws an error

enrollment['cancel_date']

以下各项工作正常:

for enrollment in enrollments:
enrollments['cancel_date'] = parse_date(enrollment['cancel_date'])

希望这个问题是有道理的。我只是很难想象所有这些表示方式。

Hopefully this question makes sense. I'm just having trouble visualizing how all of this is represented.

任何帮助将不胜感激。
谢谢。

Any help would be appreciated. Thanks.

推荐答案

我也因与此课程相关的一些麻烦而降落在这里,但未找到答案。但是我认为您已经做好了。无论如何在这里回答,以便其他人会发现这很有帮助。

I too landed up here for some troubles related to the course and found this unanswered. However I think you already managed it. Anyway answering here so that someone else might find this helpful.

就像我们都知道,可以像

Like we all know, dictionaries can be accessed like

dictionary_name['key']

以及类似的
报名['cancel_date'] 也应该起作用。

and likewise enrollments['cancel_date'] should also work.

但是如果您这样做

print enrollments

您将看到结构

[{u'status': u'canceled', u'is_udacity': u'True', ...}, {}, ... {}]

如果您注意到括号,就像一个词典列表。您可能会说这是列表的列表。尝试一下。

If you notice the brackets, it's like a list of dictionaries. You may argue it is a list of list. Try it.

print enrollments[0][0]

您会得到一个错误! KeyError

You'll get an error! KeyError.

所以,这就像词典的集合。如何访问它们?通过 enrollments [n] 向下缩放到任何词典(csv的相对行)。

So, it's like a collection of dictionaries. How to access them? Zoom down to any dictionary (rather rows of the csv) by enrollments[n].

现在您有一本字典。您现在可以自由使用密钥

Now you have a dictionary. You can now use freely the key.

print enrollments[0]['cancel_date']

现在进入循环,

for enrollment in enrollments:
    enrollment['cancel_date'] = parse_date(enrollment['cancel_date'])

这是在进行登记是捕获每个可迭代元素<$的虚拟变量c $ c>注册,例如注册[1],注册[2] ...注册[n]

What this is doing is the enrollment is the dummy variable capturing each of the iterable element enrollments like enrollments[1], enrollments[2] ... enrollments[n].

因此,每次注册都有来自注册的字典,因此 enrollment ['cancel_date'] enrollments ['cancel_date'] 上有效。

So every-time enrollment is having a dictionary from enrollments and so enrollment['cancel_date'] works over enrollments['cancel_date'].

最后,我想添加一点东西,这就是为什么我进入线程。

Lastly I want to add a little more thing which is why I came to the thread.


u'..’中 u是什么意思?例如:u'cancel_date'= u'11-02-19'。

What is the meaning of "u" in u'..' ? Ex: u'cancel_date' = u'11-02-19'.

答案是,这意味着字符串被编码为 Unicode 。它不是字符串的一部分,它是python表示法。 Unicode是一个包含世界上所有语言的字符和符号的库。

The answer is this means the string is encoded as an Unicode. It is not part of the string, it is python notation. Unicode is a library that contains the characters and symbol for all of the world's languages.

这主要是因为 unicodecsv 程序包无需跟踪和转换csv文件中的每个项目。它将它们读取为Unicode以保留所有字符。这就是Caroline和您定义并使用 parse_date()和其他函数将Unicode字符串转换为所需数据类型的原因。这都是数据整理过程的一部分。

This mainly happens because the unicodecsv package does not take the headache of tracking and converting each item in the csv file. It reads them as Unicode to preserve all characters. Now that's why Caroline and you defined and used parse_date() and other functions to convert the Unicode strings to the desired datatype. This is all a part of the Data Wrangling process.

这篇关于unicodecsv.DictReader如何表示一个csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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