gpt4 book ai didi

Python 在解析表时处理 NoneType

转载 作者:行者123 更新时间:2023-11-30 22:49:27 27 4
gpt4 key购买 nike

我正在尝试比较两个表( table_atable_b )并减去 table_a 的最后一列从table_b的最后一列开始。但是,table_a 包含一个额外的行,导致我得到 NoneType错误。我是否仍然可以包含 table_a 中的“Plums”行?并输出 NULL对于德尔塔电池?下面是我的可测试代码。

当前代码:

from datetime import datetime
import itertools

table_a = (
(datetime(2016, 9, 28, 0, 0), 'Apples', 650, 700, 850),
(datetime(2016, 9, 28, 0, 0), 'Oranges', 900, 950, 1000),
(datetime(2016, 9, 28, 0, 0), 'Grapes', 1050, 1100, 1150),
(datetime(2016, 9, 28, 0, 0), 'Plums', 2000, 3000, 4000)
)

table_b = (
(datetime(2016, 9, 27, 0, 0), 'Apples', 50, 150, 200),
(datetime(2016, 9, 27, 0, 0), 'Oranges', 250, 350, 400),
(datetime(2016, 9, 27, 0, 0), 'Grapes', 450, 550, 600),
)

table_format = '{:<10}|{:<8}|{:<8}|{:<8}|{:<8}|{:<12}'
line_sep = ('-' * 60)

print(line_sep)
print(table_format.format('Date', 'Count_1', 'Count_2', 'Count_3' , 'Count_4', 'Count_4_Delta'))


for a, b in itertools.zip_longest(table_a, table_b):
l = str(a[0])[0:10]
m = a[1]
n = a[2]
o = a[3]
p = a[4]
q = b[4]
print(line_sep)
print(table_format.format(l, m, n, o, p, (p-q)))

输出有错误:

------------------------------------------------------------
Date |Count_1 |Count_2 |Count_3 |Count_4 |Count_4_Delta
------------------------------------------------------------
2016-09-28|Apples |650 |700 |850 |650
------------------------------------------------------------
2016-09-28|Oranges |900 |950 |1000 |600
------------------------------------------------------------
2016-09-28|Grapes |1050 |1100 |1150 |550
Traceback (most recent call last):
File "/media/test.py", line 30, in <module>
q = b[4]
TypeError: 'NoneType' object is not subscriptable

如果我添加 if 语句来删除 NoneType,它会打印表格而不会出现错误,但会排除“Plums”行。

for a, b in itertools.zip_longest(table_a, table_b):
if a and b is not None:
l = str(a[0])[0:10]
m = a[1]
n = a[2]
o = a[3]
p = a[4]
q = b[4]
print(line_sep)
print(table_format.format(l, m, n, o, p, (p-q)))

带有 If 语句的输出:

------------------------------------------------------------
Date |Count_1 |Count_2 |Count_3 |Count_4 |Count_4_Delta
------------------------------------------------------------
2016-09-28|Apples |650 |700 |850 |650
------------------------------------------------------------
2016-09-28|Oranges |900 |950 |1000 |600
------------------------------------------------------------
2016-09-28|Grapes |1050 |1100 |1150 |550

我想要以下输出。其中“Plums”行仍会打印,但增量单元格具有字符串“NULL”。

期望的输出:

------------------------------------------------------------
Date |Count_1 |Count_2 |Count_3 |Count_4 |Count_4_Delta
------------------------------------------------------------
2016-09-28|Apples |650 |700 |850 |650
------------------------------------------------------------
2016-09-28|Oranges |900 |950 |1000 |600
------------------------------------------------------------
2016-09-28|Grapes |1050 |1100 |1150 |550
------------------------------------------------------------
2016-09-27|Plums |2000 |3000 |4000 |NULL

最佳答案

itertools.zip_longest接受可选的 fillvalue 参数。如果提供了,则使用它来代替 None:

>>> list(itertools.zip_longest([1, 2, 3], [4, 5]))
[(1, 4), (2, 5), (3, None)]
>>> list(itertools.zip_longest([1, 2, 3], [4, 5], fillvalue='NULL'))
[(1, 4), (2, 5), (3, 'NULL')]

您可以提供空行(NULL 值列表)作为 fillvalue:

class EmptyValue:
def __sub__(self, other):
return 'NULL'
def __rsub__(self, other):
return 'NULL'

empty_row = [None, 'NULL', EmptyValue(), EmptyValue(), EmptyValue()]
for a, b in itertools.zip_longest(table_a, table_b, fillvalue=empty_row):
...

关于Python 在解析表时处理 NoneType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39782283/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com