我正在尝试从CSV文件中提取值以创建filename:value
列表我一直在
for header, value in row.items():
AttributeError: 'list' object has no attribute 'items'
这是csv文件的结构:
filename,id,points,value
image163.jpeg,0,67,6541546.633
image206.jpeg,1,67,5873455.229
image206.jpeg,1,67,5793978.982
image207.jpeg,2,67,6978847.211
image265.jpeg,3,67,6443535.129
这是我正在运行的代码:
with open('faces.csv', 'rU') as csvfile:
facesreader = csv.reader(csvfile, delimiter=',', quotechar='|')
print(facesreader)
faces_list = {}
print(faces_list)
for row in facesreader:
print(row)
for header, value in row.items():
try:
faces_list[header].append(value)
except KeyError:
faces_list[header] = [value]
答案 0 :(得分:0)
csv.DictReader
应该可以帮助您将内容作为键值对。
<强>实施例强>
import csv
with open(filename, "r") as csvfile:
facesreader = csv.DictReader(csvfile, delimiter=',', quotechar='|')
for row in facesreader:
print(row)
<强>输出:强>
{'points': '67', 'id': '0', 'value': '6541546.633', 'filename': 'image163.jpeg'}
{'points': '67', 'id': '1', 'value': '5873455.229', 'filename': 'image206.jpeg'}
{'points': '67', 'id': '1', 'value': '5793978.982', 'filename': 'image206.jpeg'}
{'points': '67', 'id': '2', 'value': '6978847.211', 'filename': 'image207.jpeg'}
{'points': '67', 'id': '3', 'value': '6443535.129', 'filename': 'image265.jpeg'}