array and strings in Python 3

wander.lairson wander.lairson at gmail.com
Sat Dec 11 21:09:40 EST 2010


> The `array` module's handling of strings changed as well. Reading the
> Python 3 docs @ http://docs.python.org/dev/library/array.html , we
> find (all emphases added):
> class array.array(typecode[, initializer])
>    [...]
>    If given a list or string, the initializer is passed to the new
> array’s fromlist(), frombytes(), or **fromunicode()** method (see
> below) to add initial items to the array. Otherwise, the iterable
> initializer is passed to the extend() method.
> [...]
> array.fromunicode(s)
>    Extends this array with data from the given unicode string. The
> array **must be a type 'u' array**; **otherwise a ValueError is
> raised**. Use array.frombytes(unicodestring.encode(enc)) to append
> Unicode data to an array of some other type.
>
Actually I was using the 3.1 docs as reference, as it is the stable
one. After your comments,
I dug a bit more in the documentation and in the code, and I figured
out that for unicode strings,
you must pass 'u' as the first constructor parameter.

> Incidentally, if you ran 2to3 over your code and weren't warned about
> this change in the array module, then that's probably a bug in 2to3
> which ought to be reported: http://bugs.python.org
>
I am not using 2to3 because I am not converting Python 2 code to
Python 3, I am writing
code that must run on Python 2 and Python 3.

Thank you for your help :)


-- 
Best Regards,
Wander Lairson Costa
LCoN - Laboratório de Computação Natural - Natural Computing Laboratory
(http://www.mackenzie.com.br/lcon.html)
Programa de Pós-Graduação em Engenharia Elétrica (PPGEE)
Faculdade de Computação e Informática (FCI)
Universidade Presbiteriana Mackenzie - SP - Brazil



More information about the Python-list mailing list