Convert list of bytes strings to single string in Python

The bytes.join command converts list of bytes strings to a single ASCII string.

Example: in.dat is binary data with an ASCII header, so common to legacy code.

from pathlib import Path
infn = Path('in.dat')

with'rb') as f:
    line = f.readline().split()
    dat = line[-5:]
    header = line[:6]

    txt = bytes.join(b' ', header).decode('ascii')

with header=[b'this', b'is', b'a', b'yucky', b'ascii', b'header']



Leave a Comment