Dne 12.1.2012 14:46, Philippe Makowski napsal(a): > Hi, > > I need your help > > this piece of code have no problem under Python2 > #coding:utf-8 > # > import struct > > def addString(codeAsByte, s): > sLen = len(s) > format = 'cc%ds' % sLen # like 'cc50s' for a 50-byte string > newEntry = struct.pack(format, chr(codeAsByte), chr(sLen), s) > > def addInt(codeAsByte, value): > newEntry = struct.pack('ccc', chr(codeAsByte), '\x01', chr(value)) > > if __name__=='__main__': > addInt(10,10) > addString(28,'test'.encode("utf8")) > > but under Python3 I get > > struct.error: char format requires a bytes object of length 1 > > for both > > any clue ?
Yes, see: http://docs.python.org/release/3.0.1/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit Conversion from <str> to <bytes> object should fix this. regards Pavel Cisar