pytries / marisa-trie

Static memory-efficient Trie-like structures for Python based on marisa-trie C++ library.
https://marisa-trie.readthedocs.io/en/latest/
MIT License
1.03k stars 91 forks source link

Pack unicode strings #27

Closed varunpatro closed 8 years ago

varunpatro commented 8 years ago

If I have the following variables:

keys = [u'1', u'12', u'13', u'123', u'132', u'1234'] vals = [u'a', u'b', u'c', u'd', u'e', u'f'] fmt = "s" trie = marisa_trie.RecordTrie(fmt, zip(keys, vals))

But I keep getting argument for 's' must be a string

Any help?

superbobry commented 8 years ago

Hi,

AFAIK it is not possible to serialize unicode (or str in Python 3) using the struct module. You have to manually encode vals to bytes and then pass them to RecordTrie:

trie = matrisa_trie.RecordTrie("s", zip(keys, map(vals, str.encode)))