我有一个文件,该文件的标题行具有固定数量的标签和可变长度的行。最后一列的字段实际上应该是项目的子列表,但是此列表被视为列的连续体。
示例:
Name, Address, Telephone
"Bob Smith", "123 main st", "111-111-1111"
"Jon Smith", "123 main st", "111-111-1111", "222-222-2222"
我最终想遍历子列表,在这种情况下是电话号码。
我尝试使用csv dictreader,但它会删除多余的列。
谢谢。
答案 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