【问题标题】:how to ignore empty values from mysql in python to fill a list?如何在python中忽略mysql中的空值来填充列表?
【发布时间】:2020-07-11 05:01:19
【问题描述】:

所以我有一个包含人员表的数据库,并且人员具有特征 1-5。

我们用数据填充person表,我们最多可以添加5个特征,但这不是必需的

所以现在我想将每个人的特征写在一个列表中,但如果特征为空,则不会看到这些特征。 然后将结果打印出来给我。

我现在可以列出人员名单,但我仍然看到空值。

有人想通过 sql 或 python 排除空值吗?

编辑

#we fetch the result and then write it into the lists
SQLdeckung = 'Select * from Ergebnis_Compare
deckungCursor.execute(SQLdeckung)
    result_update = deckungCursor.fetchall()
    for i in result_update:
        neueTabelleResult.append(i[0])
        neueTabelleResult.append(i[1])
        neueTabelleResult.append(i[2])
        neueTabelleResult.append(i[3])
        neueTabelleResult.append(i[4])
        neueTabelleResult.append(i[5])
    deckungCursor.execute(dropNeueTabelle)


except pysql.MySQLError as err:
    print('Fehler in DeckungsCalc' , err)

connection.commit()
deckungCursor.close()
#here we want to print out the result
messagebox.showinfo('Dies ist die Deckungsliste:', neueTabelleResult)

【问题讨论】:

  • 没有代码完全无法回答。你commit()有变化吗?
  • 添加代码@roganjosh
  • 为什么在附加到neueTabelleResult 之前不检查i[x] 是否为空/空?

标签: python mysql tkinter


【解决方案1】:

您应该过滤 SQL 语句中的数据。例如:

SELECT * FROM Ergebnis_Compare WHERE <column-name> IS NOT NULL

其中<column-name> 是包含此人特征的列。

【讨论】:

    【解决方案2】:

    您可以在插入neueTabelleResult之前检查记录中的每个项目是否为空/无:

    for rec in result_update:
        for item in rec:
            if item is not None and item.strip() != '':
                neueTabelleResult.append(item.strip()) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 2021-05-18
      • 2021-08-21
      • 2012-07-11
      • 2020-09-22
      • 2021-12-31
      • 2011-03-21
      相关资源
      最近更新 更多