Conversion between strings and dictionaries in Python

Source: Internet
Author: User

Dictionary and String Conversion eval () and STR () Functions


>>> C = "{'origin _ name': U' \ u7ea2 \ u91d1 \ u9f991 ', 'material _ Code': u'000000', 'id ': 13 }"

>>> C

"{'Origin _ name': U' \ u7ea2 \ u91d1 \ u9f991 ', 'material _ Code': u'000000', 'id': 13 }"

>>> D = eval (c)

>>> D

{'Origin _ name': U' \ u7ea2 \ u91d1 \ u9f991 ', 'material _ Code': u'000000', 'id': 13}

>>> E = STR (d)

>>> E

"{'Origin _ name': U' \ u7ea2 \ u91d1 \ u9f991 ', 'material _ Code': u'000000', 'id': 13 }"




Exec statements are used to execute Python statements stored in strings or files. For example, we can generate a string containing Python code at runtime, and then execute these statements using exec statements. The following is a simple example.


>>> Exec 'print "Hello World "'

Hello World


The eval statement is used to calculate the valid Python expression stored in the string. The following is a simple example.


>>> Eval ('2*3 ')

Sometimes there are coding problems, and we need

Tagname = eval (tagname. Decode ("utf8"), or

Tagname = eval (tagname. Replace ("\ r \ n", ""). Decode ("utf8 "))



The best method is to use JSON

Try: # For python <2.6 or people using a newer versionof simplejson import simplejson as jsonworkflow t importerror: # For Python> = 2.6 import JSON

Dictionary and String Conversion eval () and STR () Functions


>>> C = "{'origin _ name': U' \ u7ea2 \ u91d1 \ u9f991 ', 'material _ Code': u'000000', 'id ': 13 }"

>>> C

"{'Origin _ name': U' \ u7ea2 \ u91d1 \ u9f991 ', 'material _ Code': u'000000', 'id': 13 }"

>>> D = eval (c)

>>> D

{'Origin _ name': U' \ u7ea2 \ u91d1 \ u9f991 ', 'material _ Code': u'000000', 'id': 13}

>>> E = STR (d)

>>> E

"{'Origin _ name': U' \ u7ea2 \ u91d1 \ u9f991 ', 'material _ Code': u'000000', 'id': 13 }"




Exec statements are used to execute Python statements stored in strings or files. For example, we can generate a string containing Python code at runtime, and then execute these statements using exec statements. The following is a simple example.


>>> Exec 'print "Hello World "'

Hello World


The eval statement is used to calculate the valid Python expression stored in the string. The following is a simple example.


>>> Eval ('2*3 ')

Sometimes there are coding problems, and we need

Tagname = eval (tagname. Decode ("utf8"), or

Tagname = eval (tagname. Replace ("\ r \ n", ""). Decode ("utf8 "))



The best method is to use JSON

Try: # For python <2.6 or people using a newer versionof simplejson import simplejson as jsonworkflow t importerror: # For Python> = 2.6 import JSON

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.