Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How can I format unicode strings?

Reply
Thread Tools

How can I format unicode strings?

 
 
gentlestone
Guest
Posts: n/a
 
      09-09-2009
return u"{}".format(self.name)

this one doesn't work on unicode strings. I there a not old formatting
style possibilty for unicode strings?

Note: self.name can be unicode string!
 
Reply With Quote
 
 
 
 
Patrick Sabin
Guest
Posts: n/a
 
      09-09-2009
gentlestone schrieb:
> return u"{}".format(self.name)


u"{0}".format(u"blah")

works for me with python-2.6.2
Maybe your format string is wrong.

- Patrick

 
Reply With Quote
 
 
 
 
Tim Northover
Guest
Posts: n/a
 
      09-09-2009
gentlestone <(E-Mail Removed)> writes:

> return u"{}".format(self.name)
>
> this one doesn't work on unicode strings. I there a not old formatting
> style possibilty for unicode strings?


It looks like you're trying to mix python 3.1 and 2.6. In 2.6 you have
to put a number inside the {} to tell it which argument to use. In 3.1
all strings are unicode.

Apparently when 2.7 is released it will backport the empty {} feature
from 3.1. Until then

return u'{0}'.format(self.name)

is what you should probably use.

Tim.
 
Reply With Quote
 
gentlestone
Guest
Posts: n/a
 
      09-09-2009
On 9. Sep., 12:31 h., Tim Northover <(E-Mail Removed)>
wrote:
> gentlestone <(E-Mail Removed)> writes:
> > *return u"{}".format(self.name)

>
> > this one doesn't work on unicode strings. I there a not old formatting
> > style possibilty for unicode strings?

>
> It looks like you're trying to mix python 3.1 and 2.6. In 2.6 you have
> to put a number inside the {} to tell it which argument to use. In 3.1
> all strings are unicode.
>
> Apparently when 2.7 is released it will backport the empty {} feature
> from 3.1. Until then
>
> return u'{0}'.format(self.name)
>
> is what you should probably use.
>
> Tim.


I have python 2.5

return u'{0}'.format(self.name)

doesn't work eigther

the error message i've got is:

'unicode' object has no attribute 'format'

is the new formatting style newer then python 2.5?

 
Reply With Quote
 
Niklas Norrthon
Guest
Posts: n/a
 
      09-09-2009
On 9 Sep, 12:49, gentlestone <(E-Mail Removed)> wrote:
>
> I have python 2.5
>
> return u'{0}'.format(self.name)
>
> doesn't work eigther
>
> the error message i've got is:
>
> 'unicode' object has no attribute 'format'
>
> is the new formatting style newer then python 2.5?


Yes. The new string formatting appeared in python 2.6. Perhaps there
is some __future__ stuff you can import to get it to work, don't know.
If not you are stuck with the old string formatting until you upgrade
to 2.6 or newer:

>>> print u'Hello %s!' % u'world'

Hello world!

/Niklas Norrthon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
os.lisdir, gets unicode, returns unicode... USUALLY?!?!? gabor Python 13 11-18-2006 09:23 AM
Unicode digit to unicode string Gabriele *darkbard* Farina Python 2 05-16-2006 01:15 PM
unicode wrap unicode object? ygao Python 6 04-08-2006 09:54 AM
Unicode + jsp + mysql + tomcat = unicode still not displaying Robert Mark Bram Java 0 09-28-2003 05:37 AM



Advertisments