简体   繁体   English

在 python 中将字节串与 islice 一起添加

[英]Adding bytestrings with islice together in python

I'm making a domain specific function based on more-itertools split_into() which fails on the last yield statement我正在基于 more-itertools split_into() 创建一个特定于域的 function,它在最后一个 yield 语句中失败

from itertools import islice
def split_into(iterable_seq, sizes):
    """
    sizes = [5]+ it.repeat(32)
    iterable_seq = b'ACAACACACCAACCCAAACACAC'
    """
    iterate = iter(iterable_seq)
    for size in sizes:
        yield b'A'*(32-size)+islice(iterate, size)

Is there a way to add the output of islice to the bytestrings?有没有办法将 islice 的 output 添加到字节串中?

Here is another way that don't work:这是另一种不起作用的方法:

yield b'A'*(32-size)+ b''.join(islice(iterate, size))

What is the difference between bytestrings of the format b'ACA' and the iterable of numbers that islice wants to put out eg [65, 67, 65]? b'ACA' 格式的字节串和 islice 想要输出的可迭代数字之间有什么区别,例如 [65, 67, 65]? It seems there shouldn't be any need for coercing the number form back to its original string form.似乎不需要将数字形式强制回其原始字符串形式。

The bytes function appears to be what you're looking for: bytes function 似乎是您要查找的内容:

yield b'A'*(32-size)+bytes(islice(iterate, size))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM