Python中None的字节连接问题


total = None
for i in range(2):
item_1 = b'x01'
item_2 = b'x02'
item_3 = b'x03'

# concatenation
combined = item_1 + item_2 + item_3 # which makes b'x01x023'

total = total + combined            # to make b'x01x023x01x023'

在上面的例子中,我得到了一个错误,因为我无法将None与Bytes连接起来。我想的一种方法是给总和一些值(比如b'x00'(,然后在总和中删除,但不确定如何做到。有人能告诉我们实现上述吗

筛选可能的None参数。filter(None,args(将返回一个可迭代的值,该值包含bool(value(==True 的所有值

def combine_bytes(*args):
return b''.join(filter(None, args))

然后您可以拨打:

combine_bytes(b'x01', None, b'x02', b'x03', False)

最新更新