这个字符串的编码是什么,'base64'还是'utf-8'??? 我怎么才能让它可读
print "4-12\u4e2a\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf".decode('base64')#no
谢谢
还有
如果我有'4-12个英文字母、数字和下划线'
我该如何得到字符串'4-12\u4e2a\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf'
是
print '4-12个英文字母、数字和下划线'.decode('what')#
我写:
print u'4-12个英文字母、数字和下划线'.encode('unicode-escape')
它打印
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
不是字符串"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)
没有'u'也是错误:
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
没事,感谢
>>> print '4-12个英文字母、数字和下划线'.decode('gb2312').encode('unicode-escape')
4-12\u4e2a\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf
5 个回答
1
这段话的意思是“4到12个英文字母、数字和下划线”,这是通过在JavaScript解释器(这里指的是WebKit检查器)中直接输入得到的。
看起来里面没有任何经过base64编码的信息。
你还有其他想知道的事情吗?
1
我想这指的是Python 3.x中对Unicode字符串的表示方式。
在Python 2.x中,你需要在Unicode字符串前面加上u""
。
0
你最后的评论:
>>> print '4-12个英文字母、数字和下划线'.decode('gb2312').encode('unicode-escape')
只有当你的源文件是以gb2312编码保存时,这样做才有效。确保在文件的顶部声明这一点,然后你就可以使用Unicode字符串了:
# coding: gb2312
print u'4-12个英文字母、数字和下划线'.encode('unicode-escape')