python编码转换实验

Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> print ord(‘A‘)

65

>>>

...

>>> a = {"a":"1","b","2"}

File "<stdin>", line 1

a = {"a":"1","b","2"}

^

SyntaxError: invalid syntax

>>> a = {"a":"1","b":"2"}

>>> str(a)

"{‘a‘: ‘1‘, ‘b‘: ‘2‘}"

>>> print a

{‘a‘: ‘1‘, ‘b‘: ‘2‘}

>>> print type(a)

<type ‘dict‘>

>>> print type(str(a))

<type ‘str‘>

>>> b = [1,2,3]

>>> print type(b)

<type ‘list‘>

>>> print type(str(b))

<type ‘str‘>

>>> str(b)

‘[1, 2, 3]‘

>>> b.__class__

<type ‘list‘>

>>> str(b).__class__

<type ‘str‘>

>>> isinstance(a, str)

False

>>> isinstance(a, dict)

True

>>> isinstance(a, unicode)

False

>>> isinstance(a, utf-8)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name ‘utf‘ is not defined

>>> isinstance(a, ‘utf-8‘)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

>>> isinstance(a, type)

False

>>> isinstance(a, unicode)

False

>>> isinstance(a, unicode)

False

>>> import chardet

>>> chardet.detect(a)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "/usr/lib/python2.6/site-packages/chardet/__init__.py", line 30, in detect

u.feed(aBuf)

File "/usr/lib/python2.6/site-packages/chardet/universaldetector.py", line 74, in feed

if aBuf[:3] == codecs.BOM:

TypeError: unhashable type

>>> chardet.detect(str(a))

{‘confidence‘: 1.0, ‘encoding‘: ‘ascii‘}

>>> chardet.detect(str(b))

{‘confidence‘: 1.0, ‘encoding‘: ‘ascii‘}

>>> c = ["我","是"]

>>> chardet.detect(str(c))

{‘confidence‘: 1.0, ‘encoding‘: ‘ascii‘}

>>> print c

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> c.encode(‘unicode‘)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

AttributeError: ‘list‘ object has no attribute ‘encode‘

>>> str(c).encode(‘unicode‘)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

LookupError: unknown encoding: unicode

>>> str(c).encode(‘utf-8‘)

"[‘\\xe6\\x88\\x91‘, ‘\\xe6\\x98\\xaf‘]"

>>> d = str(c)

>>> chardet.detect(d)

{‘confidence‘: 1.0, ‘encoding‘: ‘ascii‘}

>>> chardet.detect(c)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "/usr/lib/python2.6/site-packages/chardet/__init__.py", line 30, in detect

u.feed(aBuf)

File "/usr/lib/python2.6/site-packages/chardet/universaldetector.py", line 108, in feed

if self._highBitDetector.search(aBuf):

TypeError: expected string or buffer

>>> chardet.detect(d)

{‘confidence‘: 1.0, ‘encoding‘: ‘ascii‘}

>>> print d

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print dc

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name ‘dc‘ is not defined

>>> print c

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print d.decode(‘ascii‘)

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print type(d.decode(‘ascii‘))

<type ‘unicode‘>

>>> print d.decode(‘ascii‘)

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> chardet.detect(c.decode(‘ascii‘)

... )

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

AttributeError: ‘list‘ object has no attribute ‘decode‘

>>> chardet.detect(d.decode(‘ascii‘))

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "/usr/lib/python2.6/site-packages/chardet/__init__.py", line 25, in detect

raise ValueError(‘Expected a bytes object, not a unicode object‘)

ValueError: Expected a bytes object, not a unicode object

>>> type(d)

<type ‘str‘>

>>> print type(d.decode(‘ascii‘))

<type ‘unicode‘>

>>>  print d.decode(‘ascii‘)

File "<stdin>", line 1

print d.decode(‘ascii‘)

^

IndentationError: unexpected indent

>>> print d.decode(‘ascii‘)

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print d.decode(‘ascii‘).encode(‘utf-8‘)

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print d.decode(‘ascii‘).encode(‘utf-8‘)[0]

[

>>> print d.decode(‘ascii‘)

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> e = d.decode(‘ascii‘)

>>> print e

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> type(e)

<type ‘unicode‘>

>>> f = e.encode(‘utf-8‘)

>>> f

"[‘\\xe6\\x88\\x91‘, ‘\\xe6\\x98\\xaf‘]"

>>> print f

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> type(f)

<type ‘str‘>

>>> print f.decode("unicode_escape")

[‘‘, ‘ˉ‘]

>>> print f.encode("raw_unicode_escape")

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print f.encode("raw_unicode_escape").decode(‘utf-8‘)

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print b

[1, 2, 3]

>>> print c

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print type(c)

<type ‘list‘>

>>> print type(d)

<type ‘str‘>

>>> print d

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> import syss

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ImportError: No module named syss

>>> import sys

>>> reload(sys)

<module ‘sys‘ (built-in)>

>>> sys.setdefaultencoding(‘utf-8‘)

>>> print d

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print type(c)

<type ‘list‘>

>>> print type(d)

<type ‘str‘>

>>> cc = ["我","是"]

>>> print cc

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print type(cc)

<type ‘list‘>

>>> dd = str(cc)

>>> pirnt dd

File "<stdin>", line 1

pirnt dd

^

SyntaxError: invalid syntax

>>> print dd

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print type(dd)

<type ‘str‘>

>>> chardet.detect(d)

{‘confidence‘: 1.0, ‘encoding‘: ‘ascii‘}

>>> chardet.detect(dd)

{‘confidence‘: 1.0, ‘encoding‘: ‘ascii‘}

>>> sys.defaultencoding()

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

AttributeError: ‘module‘ object has no attribute ‘defaultencoding‘

>>> sys.defaultencoding

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

AttributeError: ‘module‘ object has no attribute ‘defaultencoding‘

>>> sys.defaultencode

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

AttributeError: ‘module‘ object has no attribute ‘defaultencode‘

>>> sys.defaultencode()

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

AttributeError: ‘module‘ object has no attribute ‘defaultencode‘

>>> sys.defaultencoding()

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

AttributeError: ‘module‘ object has no attribute ‘defaultencoding‘

>>> sys.defaultencode

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

AttributeError: ‘module‘ object has no attribute ‘defaultencode‘

>>> q = ‘中国‘

>>> type(q)

<type ‘str‘>

>>> chardet.detect(q0

... )

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name ‘q0‘ is not defined

>>> chardet.detect(q)

{‘confidence‘: 0.75249999999999995, ‘encoding‘: ‘utf-8‘}

>>> p = [‘中国‘, ‘复兴‘]

>>> chardet.detect(p)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "/usr/lib/python2.6/site-packages/chardet/__init__.py", line 30, in detect

u.feed(aBuf)

File "/usr/lib/python2.6/site-packages/chardet/universaldetector.py", line 108, in feed

if self._highBitDetector.search(aBuf):

TypeError: expected string or buffer

>>> chardet.detect(str(p))

{‘confidence‘: 1.0, ‘encoding‘: ‘ascii‘}

>>> print type(dd)

<type ‘str‘>

>>> print dd.decode(‘unicode_escape‘)

[‘‘, ‘ˉ‘]

>>> print type(dd.decode(‘unicode_escape‘))

<type ‘unicode‘>

>>> dd

"[‘\\xe6\\x88\\x91‘, ‘\\xe6\\x98\\xaf‘]"

>>> print dd

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print dd.encode(‘raw_unicode_escape‘)

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print type(dd.encode(‘raw_unicode_escape‘))

<type ‘str‘>

>>> print type(dd.encode(‘raw_unicode_escape‘).decode(‘utf-8‘))

<type ‘unicode‘>

>>> print type(dd.encode(‘raw_unicode_escape‘).decode(‘utf-8‘)

... )

<type ‘unicode‘>

>>> print dd

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print dd, type(dd)

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘] <type ‘str‘>

>>> print dd.encode(‘raw_unicode_escape‘), type(dd.encode(‘raw_unicode_escape‘))

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘] <type ‘str‘>

>>> print dd.decode(‘utf-8‘), type(dd.decode(‘utf-8‘)

... )

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘] <type ‘unicode‘>

>>> print dd.decode(‘utf-8‘)

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print dd

[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]

>>> print ee

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name ‘ee‘ is not defined

>>> ee = u"dd"

>>> ee = u"[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]"

>>> print ee

[‘‘, ‘ˉ‘]

>>> ee

u"[‘\xe6\x88\x91‘, ‘\xe6\x98\xaf‘]"

>>> ee = [u‘中国‘, u‘复兴‘]

>>> type(ee)

<type ‘list‘>

>>> print ee

[u‘\u4e2d\u56fd‘, u‘\u590d\u5174‘]

>>> print str(ee)

[u‘\u4e2d\u56fd‘, u‘\u590d\u5174‘]

>>> printee

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name ‘printee‘ is not defined

>>> print ee

[u‘\u4e2d\u56fd‘, u‘\u590d\u5174‘]

>>> print json.dumps(ee).decode(‘unicode_escape‘)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name ‘json‘ is not defined

>>> import json

>>> print json.dumps(ee).decode(‘unicode_escape‘)

["中国", "复兴"]

>>> print str(ee).decode(‘unicode_escape‘)

[u‘中国‘, u‘复兴‘]

>>> x = ‘中国‘

>>> print x

中国

>>> x

‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> type(x)

<type ‘str‘>

>>> chardet.detect(x)

{‘confidence‘: 0.75249999999999995, ‘encoding‘: ‘utf-8‘}

>>> y = x.decode(‘utf-8‘)

>>> y

u‘\u4e2d\u56fd‘

>>> print y

中国

>>> chardet.detect(y)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "/usr/lib/python2.6/site-packages/chardet/__init__.py", line 25, in detect

raise ValueError(‘Expected a bytes object, not a unicode object‘)

ValueError: Expected a bytes object, not a unicode object

>>> x

‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> x = ‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> print x

中国

>>> x

‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> x = u‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> print x

-

>>> x.decode(‘utf-8‘)

u‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> print x.decode(‘utf-8‘)

-

>>> chardet.detect(x)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "/usr/lib/python2.6/site-packages/chardet/__init__.py", line 25, in detect

raise ValueError(‘Expected a bytes object, not a unicode object‘)

ValueError: Expected a bytes object, not a unicode object

>>> print type(x)

<type ‘unicode‘>

>>> x

u‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> pirnt x

File "<stdin>", line 1

pirnt x

^

SyntaxError: invalid syntax

>>> print x

-

>>> print x.encode(‘raw_unicode_escape‘)

中国

>>> y = x.encode(‘raw_unicode_escape‘)

>>> y

‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> type y

File "<stdin>", line 1

type y

^

SyntaxError: invalid syntax

>>> type(y)

<type ‘str‘>

>>> print y

中国

>>> chardet.detect(y)

{‘confidence‘: 0.75249999999999995, ‘encoding‘: ‘utf-8‘}

>>> z = y.encode(‘utf-8‘)

>>> print z

中国

>>> z

‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> y

‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> type(z)

<type ‘str‘>

>>> type(y)

<type ‘str‘>

>>> chardet.detect(y)

{‘confidence‘: 0.75249999999999995, ‘encoding‘: ‘utf-8‘}

>>> y

‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> z = y.encode(‘utf-8‘)

>>> z = y.decode(‘utf-8‘)

>>> z

u‘\u4e2d\u56fd‘

>>> print z

中国

>>> type(z)

<type ‘unicode‘>

>>> a

u‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> f=‘\u53eb\u6211‘

>>> print f

\u53eb\u6211

>>> f

‘\\u53eb\\u6211‘

>>> type(f)

<type ‘str‘>

>>> chardet.detect(f)

{‘confidence‘: 1.0, ‘encoding‘: ‘ascii‘}

>>> f.decode(‘ascii‘)

u‘\\u53eb\\u6211‘

>>> print f.decode(‘ascii‘)

\u53eb\u6211

>>> f.decode(‘unicode_escape‘)

u‘\u53eb\u6211‘

>>> print f.decode(‘unicode_escape‘)

叫我

>>> sys.getdefaultencoding()

‘utf-8‘

>>> dd = { ‘name‘: u‘功夫熊猫‘ }

>>> print dd

{‘name‘: u‘\u529f\u592b\u718a\u732b‘}

>>> dd

{‘name‘: u‘\u529f\u592b\u718a\u732b‘}

>>> dd2 = { ‘name‘: ‘功夫熊猫‘ }

>>> dd2

{‘name‘: ‘\xe5\x8a\x9f\xe5\xa4\xab\xe7\x86\x8a\xe7\x8c\xab‘}

>>> print simplejson.dumps(dd, ensure_ascii=False)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name ‘simplejson‘ is not defined

>>> print json.dumps(dd, ensure_ascii=False)

{"name": "功夫熊猫"}

>>> print json.dumps(dd2, ensure_ascii=False)

{"name": "功夫熊猫"}

>>> print dd2

{‘name‘: ‘\xe5\x8a\x9f\xe5\xa4\xab\xe7\x86\x8a\xe7\x8c\xab‘}

>>>

时间: 2024-10-11 17:56:48

python编码转换实验的相关文章

Python开发【第三章】:Python编码转换

一.字符编码与转码 1.bytes和str 之前有学过关于bytes和str之间的转换,详细资料->bytes和str(第四字符串) 2.

Python 编码转换与中文处理

python 中的 unicode是让人很困惑.比较难以理解的问题. utf-8是unicode的一种实现方式,unicode.gbk.gb2312是编码字符集. decode是将普通字符串按照参数中的编码格式进行解析,然后生成对应的unicode对象 写python时遇到的中文编码问题: ?  /test sudo vim test.py #!/usr/bin/python #-*- coding:utf-8 -*- def weather(): import time import re i

python 编码转换

# -*-coding:gbk-*- python2.7  utf8 转gbk string="";解码   string_unicode=string.decode("utf-8") stirng_gbk=string_unicode.encode("gbk"); 字符串常用函数 split 分割字符串 为列表 string="a|b|"; string.split("|"),  join 连接字符串数组

Python之路3【知识点】白话Python编码和文件操作

Python文件头部模板 先说个小知识点:如何在创建文件的时候自动添加文件的头部信息! 通过:file--settings 每次都通过file--setings打开设置页面太麻烦了!可以通过:View--选中Toolbar工具条 修改后的效果: 一.Python Script 模板第一行 这个很简单告诉系统用什么解释去解释,如果你直接用python python_file_name.py的话这个没什么影响可以不加. 但是如果想直接通过./python_file_name.py去运行的话就得加上!

妙用python之编码转换

转自i春秋 文章难易度:★★ 知识点:python.编码转换 前 言 在日常渗透,漏洞挖掘,甚至是CTF比赛中,会遇到各种编码,常常伴随着这些编码之间的各种转换.记得刚入门那个时候,自己处理编码转换问题往往是"百度:url解码.base64加密.hex--",或者是使用一款叫做"小葵多功能转换工具"的软件,再后来直接上Burpsuite的decoder功能,感觉用的还挺好的.不过,也遇到些问题:在线转换效率低(搜索占去了2/3的时间).两款工具存在一些小问题,比如b

多线程批量转换文件编码, 从GBK, GB2312编码转换到UTF-8编码(Python)

# coding=utf-8 # author:Jeffrey Ma # version:0.1 # build 2 # created on:2015年3月31日 # description: 1. 批量转换文件编码,从GBK GB2312编码转换到UTF-8编码 # 2. 支持指定目录下所有的文件的转换,包括子目录中的文件 # 3. 支持检测原始编码,对已经是UTF-8编码的文件,不做转换 # 4. 支持只转换指定扩展名的编码 # 5. 支持多线程转换和控制台输出 # 6. 支持控制台显示线

Python字符编码转换Unicode和str

参考链接1:https://blog.csdn.net/VictoriaW/article/details/75314737 参考链接2:https://blog.csdn.net/sheldonwong/article/details/86684761 Unicode和str ## str 我们平时写的用引号括起来的字符串都是str类型的. >>> x = '哈哈' >>> x '\xb9\xfe\xb9\xfe' ### 根据上面的打印结果,可以知道str类型的x存

python基础 字符编码转换

python2 1 #python2上所有的字符编码都需要先decode到unicode,再从unicode encode到目标编码 2 str_utf8 = "我就是我" 3 print("str_utf-8:我就是我:",str_utf8) 4 #将utf-8转换为unicode 5 str_utf8_to_unicode = str_utf8.decode("utf-8") 6 print(str_utf8_to_unicode) 7 #将

Python学习之路4 - 文件操作&amp;编码转换

文件操作 文件操作大概分三步: 把文件打开. 操作文件. 把文件关上. 打开文件 打开文件用open()函数,打开成功后返回一个资源,具体语法如下. open(要打开的文件,打开方式,打开文件的格式,默认为utf-8) #例如 f = open('passengers.txt','r',encoding='utf-8') 上例用open以只读的模式打开文件,因为该文本是utf-8编码的,所以第三个参数是utf-8 w 模式是写入,是创建一个新文件的写,所以如果已经有了该文件就会被覆盖掉,注意安全