简体   繁体   中英

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

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?

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]? 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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