开发者

what is the encoding of this string,'base64' or 'utf-8'??? ,how can i get it readable

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个英文字母、数字和下划线'

ho开发者_如何学Pythonw 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


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个英文字母、数字和下划线


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?


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.


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.


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')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜