简体   繁体   English

为什么我的代码读取CSV文件,打印字典而不是列表?

[英]Why does my code, reading a CSV file, print a dictionary, and not a list?

import unicodecsv

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

enrollments = read_csv('enrollments.csv')
daily_engagement = read_csv('daily-engagement.csv')
project_submissions = read_csv('project-submissions.csv')

In the previous lesson I was learning how to read a CSV file and convert it to an editable format. 在上一课中,我正在学习如何读取CSV文件并将其转换为可编辑格式。 The code works great but when I was trying to understand the code, and I don't understand why, when I print enrollments[0] , that the result is a dictionary. 该代码很好用,但是当我试图理解该代码时,却不明白为什么当我打印enrollments[0] ,结果是字典。

The first part of the function uses with open(filename, 'rb') as f: I understand that it is used to open the file. 函数的第一部分with open(filename, 'rb') as f:我知道它用于打开文件。

The next part is reader = unicodecsv.DictReader(f) , I understand that it is used for mapping the information read, into a dictionary. 下一部分是reader = unicodecsv.DictReader(f) ,我知道它用于将读取的信息映射到字典中。

The third part is where I'm having difficulties understanding, the return list(reader) line. 第三部分是我难以理解的地方,即return list(reader)行。 Doesn't this line mean that the reader variable is returned as a list() ? 这行难道不是将reader变量作为list()返回吗?

So why it is returning a dictionary when I use print? 那么,为什么在使用print时它返回字典?

print enrollments[0]

{u'account_key': u'448',
 u'cancel_date': u'2015-01-14',
 u'days_to_cancel': u'65',
 u'is_canceled': u'True',
 u'is_udacity': u'True',
 u'join_date': u'2014-11-10',
 u'status': u'canceled'}

enrollments is a list 入学人数清单

enrollments[0] is a dict 招生人数[0]是字典

So enrollments is a list of dictionaries. 因此,入学是词典列表。

Everything worked as expected, read_csv returns a list (of dictionaties) 一切都按预期工作,read_csv返回一个列表(字典)

the second part reader = unicodecsv.DictReader(f) I understand that it is used for maping the information read into a dict. 第二部分reader = unicodecsv.DictReader(f)我知道它用于将读取的信息映射到dict中。

You are close , but not quite correct. 您很亲近 ,但不太正确。

DictReader() puts each row in the input CSV file into a dictionary. DictReader()将输入CSV文件中的每一行放入字典中。 So you get a series of dictionaries, no a single dictionary. 因此,您会得到一系列字典,而没有一个字典。 You got those rows by iterating , like using a for loop ( for row in reader: ... ). 您可以通过迭代获得这些行,例如使用for循环( for row in reader: ... )。 list() uses reader as an iterator to create a list of all the rows. list()使用reader作为迭代器来创建所有行的列表。

return list(reader) In here ain't I telling the computer to return the variable reader as a list? return list(reader)在这里,我不是告诉计算机将变量阅读器作为列表返回吗?

Yes, you are passing the reader object to list() , which produces a list object from all the rows in the file . 是的,您正在将reader对象传递给list() ,后者从file中的所有行中生成一个列表对象。 Each row is converted to a dictionary, so the end result is a list of dictionaries . 每行都将转换为字典,因此最终结果是字典列表 You could see this as result = [] for row in reader: result.append(row) , and the result list is what is returned from the function. 您可以for row in reader: result.append(row)其显示为result = [] for row in reader: result.append(row) result列表就是该函数返回的result

You then take that list, and index the list with 0 , so you ask for the first entry in the list: 然后,您获取该列表,并使用0 索引该列表,因此您要求列表中的第一项:

print enrollments[0]

This doesn't print the whole list, this prints one element from the list . 这不会打印整个列表,而是列表中打印一个元素 And that one element is a dictionary because that's the data for the first row of data in the file. 该元素是一个字典,因为这是文件中第一行数据的数据。

If you wanted to print the list itself, use 如果要打印列表本身,请使用

print enrollment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM