views:

269

answers:

5
print "4-12\u4e2a\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf".decode('base64')#no

thanks


and

if i have '4-12个英文字母、数字和下划线'

how can i get the string '4-12\u4e2a\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf'

is

print '4-12个英文字母、数字和下划线'.decode('what')#

i write:

print u'4-12个英文字母、数字和下划线'.encode('unicode-escape')

it print

4-12\xb8\xf6\xd3\xa2\xce\xc4\xd7\xd6\xc4\xb8\xa1\xa2\xca\xfd\xd7\xd6\xba\xcd\xcf\xc2\xbb\xae\xcf\xdf

not the string "4-12\u4e2a\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf

print u'4-12个英文字母、数字和下划线'.decode('utf-8').encode('unicode-escape')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "encodings\utf_8.pyo", line 16, in decode
UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-27: ordinal not in range(128)

no 'u' is also error:

print '4-12个英文字母、数字和下划线'.decode('utf-8').encode('unicode-escape')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "encodings\utf_8.pyo", line 16, in decode
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb8 in position 4: unexpected code byte

it's ok,thanks

>>> print '4-12个英文字母、数字和下划线'.decode('gb2312').encode('unicode-escape')
4-12\u4e2a\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf
+1  A: 

It's a Unicode representation. Try .decode('unicode-escape').

EDIT:

For the second decode, what you use depends on your terminal/console settings. 'utf-8' is a sane starting point, then encode using 'unicode-escape' in order to get the Unicode escape sequences.

Ignacio Vazquez-Abrams
thanks,but not print the right string.
zjm1126
Aaaand... what *is* it printing?
Ignacio Vazquez-Abrams
it print '4-12\xb8\xf6\xd3\xa2\xce\xc4\xd7\xd6\xc4\xb8\xa1\xa2\xca\xfd\xd7\xd6\xba\xcd\xcf\xc2\xbb\xae\xcf\xdf'
zjm1126
You *did* read the part that says that you need to decode with `'utf-8'` first, right?
Ignacio Vazquez-Abrams
hi Ignacio,i did it,but also error,see the code above.
zjm1126
Do note that your original did *not* have the `u` prefix.
Ignacio Vazquez-Abrams
no 'u' prefix is also error.
zjm1126
Ah, I see why. your terminal is `'gb2312'`, not 'utf-8'.
Ignacio Vazquez-Abrams
+1  A: 

It's encoded as a python unicode literal:

>>> print u"4-12\u4e2a\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf"
4-12个英文字母、数字和下划线
sth
Close, but no. It is in fact a str literal, containing a unicode repr.
Ignacio Vazquez-Abrams
@Ignacio: Well, as it's written it's a str literal that gets decoded as base64. Clearly that's not correct, who knows if it really is supposed to be a str literal. Maybe just adding a `u` to make it a unicode literal is what the OP is looking for.
sth
+1  A: 

That string says "4-12个英文字母、数字和下划线", by just typing it in to a JavaScript interpreter (in this case, the WebKit inspector).

It does not appear to have any base64 encoded information in it.

Was there something else that you wanted to know?

Brian Campbell
+1  A: 

I guess it is python 3.x representation of unicode string.

In python 2.x you will need u"" at the start of unicode string.

S.Mark
A: 

Your final comment:

>>> print '4-12个英文字母、数字和下划线'.decode('gb2312').encode('unicode-escape')

would only work if your source file is saved in gb2312 encoding. Make sure you declare that at the top of your file, then you can use Unicode strings:

# coding: gb2312
print u'4-12个英文字母、数字和下划线'.encode('unicode-escape')
Mark Tolonen