【发布时间】:2017-09-03 03:31:23
【问题描述】:
我正在制作一个程序,该程序获取销售人员报告,遍历它们并在烧瓶应用程序中显示它们。
from flask import Flask
from flask import render_template
import csv
import collections
app = Flask(__name__)
# odict_keys(['Edit', 'Activity ID', 'Assigned', 'Subject', 'Last Modified Date', 'Date', 'Priority'
# , 'Status', 'Company / Account', 'Created By', 'Activity Type', 'Comments'])
@app.route('/')
def hello_world():
reports = {}
with open('./reports/report040717.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile, delimiter=',', quotechar='"')
for row in reader:
temp = {row['Activity ID']: {'subject': row['Subject'], 'due_date': row['Date'], 'last_modified': row['Last Modified Date'], 'status': row['Status'],
'company': row['Company / Account'], 'type': row['Activity Type'], 'comments': row['Comments']}}
reports.update(temp)
reports = collections.OrderedDict(reversed(list(reports.items())))
for k, v in reports.items():
print(k, v)
return render_template('home.html', reports=reports)
if __name__ == '__main__':
app.run()
我将读取的所有行存储到一个字典中,然后将该字典推入另一个以 ID 作为键的字典。
问题是我不断得到一个空字典,我不知道如何删除它。
这是在调用渲染之前打印 k ,v 值时它的显示方式
None {'subject': None, 'due_date': None, 'last_modified': None, 'status': None, 'company': None, 'type': None, 'comments': None}
这就是所有其他人的表现方式
00TF000003Ti9iE {'subject': 'some text', 'due_date': '4/18/2017', 'last_modified': '8/23/2016', 'status': 'Not Started', 'company': 'some text', 'type': 'some text', 'comments': 'some text'}
关于如何删除报告字典中的 None 条目有什么建议吗?
【问题讨论】:
-
报告应该是一个列表而不是字典吗?当您调用 reports.update 时,您不会向报告字典添加新信息,而是用新信息替换旧信息。
标签: python python-3.x dictionary collections