我是编程新手,所以请原谅我在编码方面的浅薄知识。我有一个可以用excel打开的.csv文件。每一行代表一个人的姓名和他们的详细信息(如地址,电话号码和年龄),每个细节在不同的列中。每当我进入新的行时,这都是另一个人的细节。Python:如何将.csv文件中的信息作为一个带有元组的列表导入python?
我想将这些信息导入到python中,使得每行(即该人的每个细节)都在1个元组中(每个细节用','分隔),并且我想要列表中的所有元组。所以基本上是一个列表里面有元组的列表。
我已经从打开文件编码开始,但不知道如何实现元组中的人的每个细节细节和列表中的所有元组。我正在使用Python 2.7。
def load_friends(f):
"""
Takes the name of a file containing friends information as described in the
introduction and returns a list containing information about the friends
in the file.
load_friends(var) -> list
"""
openfile = open('friends.csv', 'Ur')
if f == 'friends.csv':
openfile = open('friends.csv', 'Ur')
lines = openfile.readlines()
print lines
openfile.close()
+0
请提供你试图解析样本CSV数据。无论如何,你一定要使用[csv](http://docs.python.org/2/library/csv.html)模块。 –
2013-04-05 09:50:06