Request Help With Byte/String Problem

Paul Rubin no.email at nospam.invalid
Tue Nov 29 21:29:51 EST 2016


Wildman <best_lay at yahoo.com> writes:
>     names = array.array("B", '\0' * bytes)
> TypeError: cannot use a str to initialize an array with typecode 'B'

In Python 2, str is a byte string and you can do that.  In Python 3,
str is a unicode string, and if you want a byte string you have to
specify that explicitly, like b'foo' instead of 'foo'.  I.e.

     names = array.array("B", b'\0' * bytes)

should work.



More information about the Python-list mailing list