Python programming JSON format conversion, Else statement utilization and SetDefault method

Source: Internet
Author: User
This article mainly introduces the summary of three common techniques in Python programming, including the conversion of JSON format, the use of else statements and SetDefault methods, the need for friends can refer to the following

You can see some common trick in Python code, and here's a simple summary.
JSON string formatting

JSON strings are often used when developing Web applications, but a longer JSON string is less readable and not easy to see inside the structure. This is the time to use Python to print the JSON string nicely.

root@exp-1:/tmp# cat Json.txt {"menu": {"breakfast": {"中文版 Muffin": {"price": 7.5}, "Bread basket": {"Price": " C ":" Assortment of fresh baked fruit breads and muffins "}," fruit breads ": {" Price ": 8}}," Drink ": {' hot Tea ': {" Price ": 5 }, "Juice": {"Price": Ten, "type": ["apple", "watermelon", "orange"]}}}}root@exp-1:/tmp# root@exp-1:/tmp# cat Json.txt | Python-m json.tool{  "menu": {    "breakfast": {"      Bread basket": {        "desc": "Assortment of fresh baked fruit B Reads and muffins ",        " price ": +      },      " 中文版 Muffin ": {        " price ": 7.5      },      " Fruit breads ": {        "Price": 8      }    ,    "drink": {' hot      Tea ': {        ' price ': 5      },      ' Juice ': {' price ':        10,        "type": [          "Apple",          "watermelon",          "Orange"        ]      }    }  }}root@exp-1:/tmp#

else's Magical

In some scenarios we need to determine whether we are jumping out of a for loop and dealing with only the case where break jumps out. At this point, it is common practice to use a flag variable to identify whether to jump out of the for loop. As shown in the following example, see if there is a multiple of 17 between 60 and 80.

Flag = FalseFor Item in xrange (total):  If Item% = = 0:    flag = True    breakif flag:  print "Exists at LEAs T one number can be pided by 17 "

In fact, you can use else to achieve the same effect without introducing new variables.

For item in xrange:  if Item% = = 0:    flag = True    breakelse:  print "exist"

SetDefault method

Dictionary is a very powerful built-in data structure for Python, but it's inconvenient to use it, like we usually do in multi-layered nesting.

Dyna_routes = {}method = ' GET ' Whole_rule = none# Some other logical processing ... if method in Dyna_routes:  dyna_routes[method].append ( Whole_rule) Else:  Dyna_routes[method] = [Whole_rule]

In fact, there is a simpler way of writing that can achieve the same effect

Self.dyna_routes.setdefault (method, []). Append (Whole_rule)

Or you can use the Collections.defaultdict module

Import collectionsdyna_routes = collections.defaultdict (list) ... dyna_routes[method].append (whole_rule)

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.