【问题标题】:How do I delete a dict item that is a 'NoneType'?如何删除“NoneType”的字典项目?
【发布时间】: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


【解决方案1】:

我会想象您从 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)

【讨论】:

  • 那行得通。非常感谢你,我正试图解决这个问题。
  • 不!接受我的回答将不胜感激:)
  • 2 分钟,我会的!
【解决方案2】:

您不应该将任何内容放入仅包含 None 作为值的较大字典中。我的原始代码不正确,这是 TemporalWolf 的建议修复。

if(None not in temp):
    reports.update(temp)

【讨论】:

  • Python 允许 if temp is not None: 不需要括号...这是行不通的,因为 temp 是包含 None 答案的字典,而不是 None 本身。 if temp: 会检查它的真实性,但因为它填充了 Nones,它的长度不是零,因此它最终仍会评估为 true。
  • 你是对的,我误读了部分问题。固定为工作代码
  • 你仍然使用比你需要的更多的括号:):if None not in temp.values(): 但我想你会发现它仍然不起作用temp.values() 是一个单一的值:{'subject': None, 'due_date': None, 'last_modified': None, 'status': None, 'company': None, 'type': None, 'comments': None}。我建议改为if None not in temp:,因为in dict 默认列出他们的密钥,这将测试row['Activity ID'] 是否为None
  • 我总是为 if 语句加上括号,尽管我知道它们不是必需的。 temp.values() 是一个 None 值的数组。我提前测试了它是否可以工作。
  • 再看temp。它是一个包含单个键值对的字典。该值是另一个字典,包含字符串到值条目,格式为{k : {sk: sv, sk2: sv2, ...}}
猜你喜欢
  • 2012-08-31
  • 2011-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多