Python Code Conversion Experiment

Source: Internet
Author: User

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 = [+ +]

>>> 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):

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

Nameerror:name ' UTF ' is not defined

>>> isinstance (A, ' utf-8 ')

Traceback (most recent):

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):

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

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

U.feed (ABUF)

File "/usr/lib/python2.6/site-packages/chardet/universaldetector.py", line-up, 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 = ["I", "yes"]

>>> Chardet.detect (str (c))

{' Confidence ': 1.0, ' Encoding ': ' ASCII '}

>>> Print C

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

>>> c.encode (' Unicode ')

Traceback (most recent):

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

Attributeerror: ' List ' object has no attribute ' encode '

>>> Str (c). Encode (' Unicode ')

Traceback (most recent):

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):

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

File "/usr/lib/python2.6/site-packages/chardet/__init__.py", line +, 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):

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):

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

Attributeerror: ' List ' object has no attribute ' decode '

>>> Chardet.detect (D.decode (' ASCII '))

Traceback (most recent):

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

File "/usr/lib/python2.6/site-packages/chardet/__init__.py", line +, 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")

[' ', ' ˉt ']

>>> 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):

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 = ["I", "yes"]

>>> 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):

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

Attributeerror: ' Module ' object has no attribute ' defaultencoding '

>>> sys.defaultencoding

Traceback (most recent):

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

Attributeerror: ' Module ' object has no attribute ' defaultencoding '

>>> Sys.defaultencode

Traceback (most recent):

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

Attributeerror: ' Module ' object has no attribute ' Defaultencode '

>>> Sys.defaultencode ()

Traceback (most recent):

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

Attributeerror: ' Module ' object has no attribute ' Defaultencode '

>>> sys.defaultencoding ()

Traceback (most recent):

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

Attributeerror: ' Module ' object has no attribute ' defaultencoding '

>>> Sys.defaultencode

Traceback (most recent):

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

Attributeerror: ' Module ' object has no attribute ' Defaultencode '

>>> q = ' China '

>>> type (q)

<type ' str ' >

>>> Chardet.detect (q0

... )

Traceback (most recent):

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

Nameerror:name ' q0 ' is not defined

>>> Chardet.detect (q)

{' confidence ': 0.75249999999999995, ' encoding ': ' Utf-8 '}

>>> p = [' China ', ' revival ']

>>> Chardet.detect (P)

Traceback (most recent):

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

File "/usr/lib/python2.6/site-packages/chardet/__init__.py", line +, 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 ')

[' ', ' ˉt ']

>>> 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):

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

[' ', ' ˉt ']

>>> EE

U "[' \xe6\x88\x91 ', ' \xe6\x98\xaf ']"

>>> EE = [u ' china ', U ' revival ']

>>> 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):

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):

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

Nameerror:name ' JSON ' is not defined

>>> Import JSON

>>> print json.dumps (EE). Decode (' Unicode_escape ')

["China", "revival"]

>>> print str (EE). Decode (' Unicode_escape ')

[u ' China ', U ' revival ']

>>> x = ' China '

>>> Print X

China

>>> 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

China

>>> Chardet.detect (y)

Traceback (most recent):

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

File "/usr/lib/python2.6/site-packages/chardet/__init__.py", line +, 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

China

>>> 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):

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

File "/usr/lib/python2.6/site-packages/chardet/__init__.py", line +, 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 ')

China

>>> 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

China

>>> Chardet.detect (y)

{' confidence ': 0.75249999999999995, ' encoding ': ' Utf-8 '}

>>> z = y.encode (' Utf-8 ')

>>> Print Z

China

>>> 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

China

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

Call me

>>> sys.getdefaultencoding ()

' Utf-8 '

>>> dd = {' name ': U ' Kung Fu Panda '}

>>> print DD

{' Name ': U ' \u529f\u592b\u718a\u732b '}

>>> DD

{' Name ': U ' \u529f\u592b\u718a\u732b '}

>>> dd2 = {' name ': ' Kung Fu Panda '}

>>> DD2

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

>>> Print Simplejson.dumps (DD, Ensure_ascii=false)

Traceback (most recent):

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

Nameerror:name ' Simplejson ' is not defined

>>> Print Json.dumps (DD, Ensure_ascii=false)

{"Name": "Kung Fu Panda"}

>>> Print Json.dumps (DD2, Ensure_ascii=false)

{"Name": "Kung Fu Panda"}

>>> Print DD2

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

>>>


Python Code Conversion Experiment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.