Re: Python & Unicode
[email protected] Fri, 10 Mar 2006 18:00:04 -0500
| Newsgroups | gmane.comp.db.metakit |
|---|---|
| Message-ID | <[email protected]> |
> > Thanks for the reply. Type S gives "not a python string", type B gives
> > "wrong type for ByteProp". Here is the output:
> >
> > >>> db = metakit.storage("test.db", 2)
> > >>> v = db.getas("test[name:B,test:S]")
> > >>> v.append(name='abc',test='def')
> > 0
> > >>> for row in v: print row.name, row.test
> > ...
> > abc def
> > >>> v.append(name=u'abc',test='def')
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in ?
> > TypeError: wrong type for ByteProp
> > >>> v.append(name='abc',test=u'def')
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in ?
> > TypeError: not a Python string
>
> You are sending a Python unicode object.
>
> You need to send an array of bytes.
>
> ------------------------------------------------------------------------
>
> import metakit
>
> db = metakit.storage()
> people = db.getas('[name:S,namei:B]')
>
> u16be = '\x00\x4a\x00\x6f\x00\xe5\x00\x6f'
>
> people.append(name='Joao',namei=u16be)
>
> print people[0].name
> print list(people[0].name)
>
> print people[0].namei
> print list(people[0].namei)
>
> print people[0].namei.decode('utf-16be')
>
> ------------------------------------------------------------------------
>
> C:\>unimeta
> Joao
> ['J', 'o', 'a', 'o']
> J o s o
> ['\x00', 'J', '\x00', 'o', '\x00', '\xe5', '\x00', 'o']
> Joåo
>
> ------------------------------------------------------------------------
If you're still lost, the additional u'abc' example I added may help.
ie. Your code will work if updated with u'def'.encode('utf-16be')
import metakit
db = metakit.storage()
people = db.getas('[name:S,namei:B]')
u16be = '\x00\x4a\x00\x6f\x00\xe5\x00\x6f'
people.append(name='Joao',namei=u16be)
print people[0].name
print list(people[0].name)
print people[0].namei
print list(people[0].namei)
print people[0].namei.decode('utf-16be')
print people[0].namei.decode('utf-16be').encode('cp437')
print people[0].namei.decode('utf-16be').encode('utf-16be')
print list(u16be)
print list(people[0].namei)
uabc = u'abc'
people.append(name='abc',namei=uabc.encode('utf-16be'))
print people[1].namei
print people[1].namei.decode('utf-16be')
--
kai
_____________________________________________
Metakit mailing list - [email protected]
http://www.equi4.com/mailman/listinfo/metakit