Python:如何将.csv文件中的信息导入python作为包含元组的列表?

use*_*286 2 python import function

我是编程新手,所以请原谅我的编码浅薄知识.我有一个.csv文件,我可以用excel打开.每行代表一个人的姓名及其详细信息(如地址,电话号码和年龄)以及不同列中的每个详细信息.每当我移动到一个新行时,它都是另一个人的细节.

我想将这些信息导入到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()
Run Code Online (Sandbox Code Playgroud)

eum*_*iro 11

使用该csv模块非常简单:

import csv

with open('friends.csv', 'Ur') as f:
    data = list(tuple(rec) for rec in csv.reader(f, delimiter=','))
Run Code Online (Sandbox Code Playgroud)

data 是一个元组列表.

csv模块正确读取文件:

"Smith, John",123
Run Code Online (Sandbox Code Playgroud)

将被视为

('Smith, John', '123')
Run Code Online (Sandbox Code Playgroud)