Pavel Cisar | 12 Jan 18:22 2012
Picon

Re: pack problem in Python 3

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

------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/firebird-python/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/firebird-python/join
    (Yahoo! ID required)

<*> To change settings via email:
    firebird-python-digest@... 
    firebird-python-fullfeatured@...

<*> To unsubscribe from this group, send an email to:
    firebird-python-unsubscribe@...

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/


Gmane