I have the following code (as an example) in a unittest TestCase
def test(self):
a = array('u','\0'*3)
a[0] = 'h'
a[1] = 'h'
a[2] = 'h'
self.assertEqual(a.tostring(), "hhh")
The assertion fails with the following error:
AssertionError: b'h\x00\x00\x00h\x00\x00\x00h\x00\x00\x00' != 'hhh'
Now I understand that the array I created is for Unicode characters which are 4 bytes long hence the extra 3 NUL bytes for every character I entered. My questions are:
- Can I convert the string “hhh” into Unicode representation inline of my assert?
- Is there a ascii option to create my array with?
EDIT: to answer the questions that have come up:
1. I am using Python 3
2. array comes from module array, can be imported with: from array import array
I suppose you are working with Python3, which seems to lack a
'c'option forarray.In this case, I would do
Another option would be
But then you have a unicode string and not a
bytes()object.