Strings show as brackets with a 'u'.

G

goldtech

Hi,

Probably newbie question but not sure how suppress the brackets and
the 'u' ? I assume pyhon is telling me it's a unicode string in the n
variable.

I'm using using Idle on winXP, activestate 2.7. Is there a way to
suppress this and just show 174 in the shell ?
A script reading data and assigns 174 to n via some regex. Links on
this appreciated - I've tried to understand unicode before, will keep
trying...thanks.
 
R

rantingrick

[u'174']

Probably newbie question but not sure how suppress the brackets and
the 'u' ? I assume pyhon is telling me it's a unicode string in the n
variable.

Try type(n) and see what happens. Then report back. :)
 
C

Chris Angelico

 I'm using using Idle on winXP, activestate 2.7. Is there a way to
suppress this and just show 174  in the shell ?
A script reading data and assigns 174 to n via some regex. Links on
this appreciated - I've tried to understand unicode before, will keep
trying...thanks.

There's two things there. Firstly, your regex is returning a list, not
a string; and secondly, you are seeing repr(n) instead of just its
content. Try:

This should print just the value.

(Pro tip: rantingrick is a troll. You can safely ignore him.)

Chris Angelico
 
T

Thomas Jollans

It's probably a list containing a single unicode string.

You can pull the first element from the list with n[0].

To print a unicode string in 2.x without the u stuff:

print u'174'.encode('ISO-8859-1')
just
will do.

Encoding the string by hand is only useful if Python doesn't know the
terminal's encoding, but you do.
On Sat, Jul 23, 2011 at 5:33 PM, goldtech <[email protected]


Hi,

Probably newbie question but not sure how suppress the brackets and
the 'u' ? I assume pyhon is telling me it's a unicode string in the n
variable.

I'm using using Idle on winXP, activestate 2.7. Is there a way to
suppress this and just show 174 in the shell ?
A script reading data and assigns 174 to n via some regex. Links on
this appreciated - I've tried to understand unicode before, will keep
trying...thanks.
 
U

Ulrich Eckhardt

Chris said:
I'm using using Idle on winXP, activestate 2.7. Is there a way to
suppress this and just show 174 in the shell ?
A script reading data and assigns 174 to n via some regex. Links on
this appreciated - I've tried to understand unicode before, will keep
trying...thanks.

There's two things there. Firstly, your regex is returning a list, not
a string; and secondly, you are seeing repr(n) instead of just its
content. Try:

This should print just the value.

(Pro tip: rantingrick is a troll. You can safely ignore him.)

If he's a troll, he's one of the better trolls here IMHO because he gave the
best advise in this thread. You just gave the OP a fish, he provided a
valuable advise on fishing itself.

Uli
 
R

rantingrick

Chris said:

You just gave the OP a fish, he provided a
valuable advise on fishing itself.

I always believed the best way to teach someone is not to give them a
direct answer. No. Instead i like to offer clues so that the person
can utilize his own problem solving skills to find the answer. Because
the most important skill a person can have in the programming field is
the ability to solve complex problems by breaking them down into their
smallest units; tackling each unit; and finally assembling the puzzle
in a cohesive and intelligent manner.

Anyway the OP may want to check out my recent thread regarding
Python's Built-in Functions.
http://groups.google.com/group/comp.lang.python/browse_thread/thread/f419d4e11f27882e?hl=en#
 
G

goldtech

Thank you. So what is happening? Is it that I'm in an environment that
is not unicode and python is telling me the string (ie. n[0]) is
unicode? (I'll do a hatchet-job on this subject if I ask anymore about
unicode). Thanks to posters for their patience!
 
T

Terry Reedy

Thank you. So what is happening? Is it that I'm in an environment that
is not unicode and python is telling me the string (ie. n[0]) is
unicode?

Yes, n is a list and n[0] is a unicode string and you are using some
2.x, which is ascii/byte string based. Python 3 is unicode based.

If you do not *need* to use 2.x and are just learning Python, I
personally recommend starting with Python 3. Others agree with me, still
other do not. I would add 'especially if you want to use unicode'.

Rick gave you some good advice, perhaps worth re-reading. What you need
are investigative skills, and not just bits of data.
 
S

Steven D'Aprano

Thank you. So what is happening? Is it that I'm in an environment that
is not unicode and python is telling me the string (ie. n[0]) is
unicode? (I'll do a hatchet-job on this subject if I ask anymore about
unicode). Thanks to posters for their patience!

It's hard to say with no context remaining. I take it you're printing n[0]
and getting unexpected results?

To start with, try calling this: type(n[0])

That will tell you if the object is a byte-string, or unicode.

I don't think there is any way to tell the console's encoding directly from
Python, although you can look at sys.stdout.encoding which will tell you
what Python thinks it is.

I think that from Windows you can run chcp.exe to see the console encoding,
although I can't confirm that. From Linux, there's probably a bazillion
different ways, none of which guaranteed to be either correct or consistent
with the others. But I'm not bitter.

You can try with the default locale encoding, or the LANG environment
variable:

locale charmap
echo $LANG

but that only tells you what your shell thinks the encoding should be, not
what your terminal is actually using.

Your console app (xterm, konsole, Gnome terminal, whatever...) probably has
a way to query what encoding it is using, but I'll be buggered if I can
find out what that is. In konsole I can look at the Settings > Encodings
menu, but it claims to be using "default". How very useful.
 
G

goldtech

Rick gave you some good advice, perhaps worth re-reading. What you need
are investigative skills, and not just bits of data.


Totally agree. beginning to see what's going on. I'm not specifically
accessing the list element with n (vs. n[0]). Printing n uses repr
which gives the unicode "tag". Something akin to this...thanks for
showing what i need to read up on...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top