如何从csv中读取多行

时间:2016-04-03 21:40:46

标签: python csv list-comprehension

我有一个csv文件,其中包含姓名和电话号码。我只对我的电话号码感兴趣。

for (int i = 0; i < Numbers.length; i++) {
    System.out.print(Numbers[i] + (i%20 != 0 || i == 0 ? " " : "\n"));
}

但我也想要各自的电话号码,我试试这个:

with open(phone_numbers) as f:
    reader = csv.DictReader(f)
    names =  [record['Name'] for record in reader if record['phone']]

但是对于第二个项目,我得到一个空字符串,我猜测user_data = {} with open(phone_numbers) as f: reader = csv.DictReader(f) user_data['Name'] = [record['Name'] for record in reader if record['phone']] user_data['phone'] = [record['phone'] for record in reader if record['phone']] 是一个生成器,这就是为什么我可以迭代它两次。

我尝试使用元组,但只是按照这种方式工作:

record

在这种情况下,我有两个变量user_data = {} with open(phone_numbers) as f: reader = csv.DictReader(f) user_data['Name'] = [(record['Name'],record['phone']) for record in reader if record['phone']] phone存储在Name中,这不是我想要的。

如果我试试这个:

user_data['Name']

我收到以下错误:

user_data = {} with open(phone_numbers) as f: reader = csv.DictReader(f) user_data['Name'],user_data['phone'] = [(record['Name'],record['phone']) for record in reader if record['phone']]

编辑:

这是表格的一个示例:

+ -------- + --------------- +
|电话|号码|
+ -------- + -------- +
|路易斯| 000 111 22222 |
+ -------- + -------- +
|保罗| 000 222 3333 |
+ -------- + -------- +
|安德里亚| |
+ -------- + -------- +
|豪尔赫| 111 222 3333 |
+ -------- + --------------- +

因此所有行都有一个名称,但并非所有行都有电话。

6 个答案:

答案 0 :(得分:1)

你的猜测是对的。如果这是你想要的方法 - 迭代两次,你应该使用seek(0)

reader = csv.DictReader(f)
user_data['Name'] =  [record['Name'] for record in reader if record['phone']]
f.seek(0)   # role back to begin of file ...
reader = csv.DictReader(f)
user_data['phone'] = [record['phone'] for record in reader if record['phone']]

但是,这不是很有效,您应该尝试将数据放在一个卷上。以下内容应该在一个卷中进行:

user_data = {}

def extract_user(user_data, record):
    if record['phone']:
        name = record.pop('name')
        user_data.update({name: record})

[extract_user(user_data, record) for record in reader]

示例:

In [20]: cat phones.csv
name,phone
hans,01768209213
grettel,
henzel,123457123

In [21]: f = open('phones.csv')

In [22]: reader = csv.DictReader(f)

In [24]: %paste
user_data = {}

def extract_user(user_data, record):
    if record['phone']:
        name = record.pop('name')
        user_data.update({name: record})

[extract_user(user_data, record) for record in reader]

## -- End pasted text --
Out[24]: [None, None, None]

In [25]: user_data
Out[25]: {'hans': {'phone': '01768209213'}, 'henzel': {'phone': '123457123'}}

答案 1 :(得分:1)

我认为有一个更简单的方法因为它是一个csv文件,因为你指出的列标题然后每行都有一个值的电话,它或者什么都没有 - 所以这个测试没什么,如果没有什么可以将名称和电话添加到user_data

import csv
user_data = []
with open(f,'rb') as fh:
   my_reader = csv.DictReader(fh)
   for row in my_reader:
       if row['phone'] != ''
           user_details = dict()
           user_details['Name'] = row['Name']
           user_details['phone'] = row['phone']
           user_data.append(user_details)

通过使用DictReader我们让魔法发生,所以我们不必担心寻求等。

如果我不理解,你想要一本字典那么容易

import csv
user_data = dict()
with open(f,'rb') as fh:
   my_reader = csv.DictReader(fh)
   for row in my_reader:
       if row['phone'] != ''
           user_data['Name'] = row['phone']

答案 2 :(得分:1)

您正在寻找的内容是否有可能丢失数据文件中的某些信息?

In [26]: !cat data00.csv
Name,Phone,Address
goofey,,ade
mickey,1212,heaven
tip,3231,earth

In [27]: f = open('data00.csv')

In [28]: r = csv.DictReader(f)

In [29]: lod = [{'Name':rec['Name'], 'Phone':rec['Phone']} for rec in r if rec['Phone']]

In [30]: lod
Out[30]: [{'Name': 'mickey', 'Phone': '1212'}, {'Name': 'tip', 'Phone': '3231'}]

In [31]: 

另一方面,如果您的文件只包含NamePhone列,那么它应该是 刚

In [31]: lod = [rec for rec in r if rec['Phone']]

答案 3 :(得分:1)

您可以使用dict将元组列表转换为字典。如果您的记录没有get值,也需要使用phone

import csv

user_data = {}
with open(phone_numbers) as f:
    reader = csv.DictReader(f)
    user_data = dict([(record['Name'], record['phone']) for record in reader if record.get('phone').strip())

如果您想单独列出namephone,可以使用* 表达式

with open(phone_numbers) as f:
    reader = csv.DictReader(f)
    names, phones = zip(*[(record['name'], record['value']) for record in reader if record.get('phone').strip()])

答案 4 :(得分:1)

我通常使用行索引:

input = open('mycsv.csv', 'r')
user_data = {}

for row in csv.reader(input):
    if row[<row # containing phone>]:
        name = row[<row # containing name>]
        user_data[name] = row[<row # containing phone>]

答案 5 :(得分:1)

除了打开包装外,你一直都是正确的。

result = [(record["name"], record["phone"]) for record in reader if record["phone"]]
# this gives [(name1, phone1), (name2,phone2),....]

你必须[dostuff for name, phone in result]而不是name,phone = result,这在语义和语法上都没有意义。