How to concatenate unicode strings ???
Algis Kabaila
akabaila at pcug.org.au
Tue Apr 26 14:03:29 EDT 2011
On Wednesday 27 April 2011 02:33:00 Ariel wrote:
> with commands.getoutput(one_comand.encode('utf-8')) it works
> !!!
>
> On Tue, Apr 26, 2011 at 6:22 PM, Ariel <isaacrc82 at gmail.com>
wrote:
> > And what about if after the string is concat I want it to
> > pass is to the command line to do anything else, for
> > instance:
> > one_command = cadena.decode('utf-8') +
> > cadena1.decode('utf-8') commands.getoutput(one_comand)
> >
> > But I receive this error:
> >
> > Traceback (most recent call last):
> > File "<console>", line 1, in <module>
> > File "/usr/lib/python2.6/commands.py", line 46, in
> > getoutput
> >
> > return getstatusoutput(cmd)[1]
> >
> > File "/usr/lib/python2.6/commands.py", line 55, in
> > getstatusoutput
> >
> > pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
> >
> > UnicodeEncodeError: 'ascii' codec can't encode character
> > u'\xf1' in position 31: ordinal not in range(128)
> >
> > How could I solve that ???
> > Regards
> > Ariel
> >
> > On Tue, Apr 26, 2011 at 6:07 PM, Chris Rebert
<clp2 at rebertia.com> wrote:
> >> On Tue, Apr 26, 2011 at 8:58 AM, Ariel
<isaacrc82 at gmail.com> wrote:
> >> > Hi everybody, how could I concatenate unicode strings
> >> > ??? What I want to do is this:
> >> >
> >> > unicode('this an example language ') +
> >> > unicode('español')
> >> >
> >> > but I get an:
> >> >
> >> > Traceback (most recent call last):
> >> > File "<console>", line 1, in <module>
> >> >
> >> > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3
> >> > in position 11: ordinal not in range(128)
> >> >
> >> > How could I concatenate unicode strings ???
> >>
> >> That error is from the 2nd call to unicode(), not from the
> >> concatenation itself. Use proper Unicode string literals:
> >>
> >> u'this an example language ' + u'español'
> >>
> >> You'll probably also need to add the appropriate source
> >> file encoding declaration; see
> >> http://www.python.org/dev/peps/pep-0263/
> >>
> >> Cheers,
> >> Chris
> >> --
> >> http://rebertia.com
The following is from Idle3 (IDLE for Python3:
>>> 'this an example language ' + 'español'
'this an example language español'
>>>
In Python3 all strings are unicode, so your problem just does
not exist. Upgrading to Python 3 would eliminate the problem,
as the above extract demonstrates.
Perhaps it is time to upgrade to Python 3.2
In the above when I write Python 3, I mean "Python 3.1 or
higher".
With kind regards,
OldAl.
PS: I do not have Spanish on my computer, but I do have at least
one other languages that uses characters that are outside of
ascii limit of 128.
A.
--
Algis
http://akabaila.pcug.org.au/StructuralAnalysis.pdf
More information about the Python-list
mailing list