如何处理每行可变列的CSV

时间:2018-07-05 20:30:18

标签: python python-3.x

我有一个文件,该文件的标题行具有固定数量的标签和可变长度的行。最后一列的字段实际上应该是项目的子列表,但是此列表被视为列的连续体。

示例:

Name, Address, Telephone
"Bob Smith", "123 main st", "111-111-1111"
"Jon Smith", "123 main st", "111-111-1111", "222-222-2222"

我最终想遍历子列表,在这种情况下是电话号码。

我尝试使用csv dictreader,但它会删除多余的列。

谢谢。

2 个答案:

答案 0 :(得分:1)

您在DictReader docs中可以看到:

  

如果一行中的字段多于'--',则其余数据将放入列表中,并以fieldnames指定的字段名(默认为restkey)存储。

您所要做的就是传递None参数,所有多余的值都将放在那里。

restkey

将打印

with open('yourfile.csv') as f:
    cf = csv.DictReader(f, restkey='extra')
    for row in cf:
        print(row)

答案 1 :(得分:1)

您不需要DictReader。使用标准的reader和元组分配语法:

代码:

import csv

with open('test.csv') as f:
    r = csv.reader(f)
    next(r) # skip header

    # Note this assigns the 3rd and remaining columns to 'telephone' as a list.
    for name,addr,*telephone in r:
        print(f'name:     {name}')
        print(f'address:  {addr}')
        for i,phone in enumerate(telephone,1):
            print(f'Phone #{i}: {phone}')
        print()

test.csv:

Name,Address,Telephone
"Bob Smith","123 main st","111-111-1111"
"Jon Smith","123 main st","111-111-1111","222-222-2222"

输出:

name:     Bob Smith
address:  123 main st
Phone #1: 111-111-1111

name:     Jon Smith
address:  123 main st
Phone #1: 111-111-1111
Phone #2: 222-222-2222