我正在创建一个程序,它接受salesforce报告,遍历它们并在烧瓶应用程序中显示它们。
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作为键的字典中。
问题是我一直在拿一本空字典,我无法弄清楚如何删除它 这是在调用render
之前im打印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'}
有关如何删除报告中的无条目的任何建议?
答案 0 :(得分:3)
我想你会从CSV文件中读取空行。您可以执行以下操作,以便那些空行根本不会加载到字典中:
if row['Activity ID'] and row['Subject']:
# row will only be added if the values above aren't None
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)
答案 1 :(得分:1)
您不应该在只包含None作为值的较大字典中放置任何内容。我的原始代码不正确,这是TemporalWolf的建议修复。
if(None not in temp):
reports.update(temp)