This article is a summary of modern Python development: syntactical fundamentals and engineering practices. See the Python Learning & Practice Resource Index for more Python related materials. This article references Python Crash Course Cheat Sheets, Pysheeet, etc. This article only includes the key knowledge points and syntax that the author often uses in daily work. If you want to further learn Python or are interested in the direction of machine learning and data mining, you can refer to the Data Science and machine Learning Manual of The Programmer.

Basic grammar

Python is a high-level, dynamically typed, multiparadigm programming language; When defining a Python file, we usually declare the file encoding first:

# specify how the script is called
#! /usr/bin/env python
Configure utF-8 encoding
# -*- coding: utf-8 -*-

Configure other encodings
# -*- coding: <encoding-name> -*-

The following can also be used in # Vim
# vim:fileencoding=<encoding-name>Copy the code

Life is short, use Python, and lots of powerful syntactic sugar makes Python code look like pseudo-code a lot of the time. For example, the simple quicksort we implement in Python is much smaller than Java:

def quicksort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) / 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quicksort(left) + middle + quicksort(right)

print quicksort([3.6.8.10.1.2.1])
# Prints "[1, 1, 2, 3, 6, 8, 10]"Copy the code

Console interaction

The __name__ keyword is used to determine whether a script is executed directly with a Python command or by an external reference. Google’s open source Fire is also a great framework for quickly encapsulating a class as a command-line tool:

import fire

class Calculator(object):
  """A simple calculator class."""

  def double(self, number):
    return 2 * number

if __name__ == '__main__':
  fire.Fire(Calculator)

# python calculator.py double 10 # 20
# python calculator.py double --number=15 # 30Copy the code

In Python 2 print is an expression, whereas in Python 3 print is a function. If you want print to be used as a function in Python 2, you need to introduce a custom:

from __future__ import print_functionCopy the code

We can also use pprint to beautify the console output:

import pprint

stuff = ['spam'.'eggs'.'lumberjack'.'knights'.'ni']
pprint.pprint(stuff)

# Custom parameters
pp = pprint.PrettyPrinter(depth=6)
tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',))))))))
pp.pprint(tup)Copy the code

The module

Modules in Python are Python source files that export classes, functions, and global variables. When importing variables from a module, the function name is usually a Namespace. Packages in Python are modules’ folders, and __init__.py usually identifies a folder as a Package:

# file directory
someDir/
    main.py
    siblingModule.py

# siblingModule.py

def siblingModuleFun(a):
    print('Hello from siblingModuleFun')

def siblingModuleFunTwo(a):
    print('Hello from siblingModuleFunTwo')

import siblingModule
import siblingModule as sibMod

sibMod.siblingModuleFun()

from siblingModule import siblingModuleFun
siblingModuleFun()

try:
    # Import 'someModuleA' that is only available in Windows
    import someModuleA
except ImportError:
    try:
        # Import 'someModuleB' that is only available in Linux
        import someModuleB
    except ImportError:Copy the code

Package can set a uniform entry for all files in a directory:

someDir/
    main.py
    subModules/
        __init__.py
        subA.py
        subSubModules/
            __init__.py
            subSubA.py

# subA.py

def subAFun(a):
    print('Hello from subAFun')

def subAFunTwo(a):
    print('Hello from subAFunTwo')

# subSubA.py

def subSubAFun(a):
    print('Hello from subSubAFun')

def subSubAFunTwo(a):
    print('Hello from subSubAFunTwo')

# __init__.py from subDir

# Adds 'subAFun()' and 'subAFunTwo()' to the 'subDir' namespace 
from .subA import *

# The following two import statement do the same thing, they add 'subSubAFun()' and 'subSubAFunTwo()' to the 'subDir' namespace. The first one assumes '__init__.py' is empty in  'subSubDir', and the second one, assumes '__init__.py' in 'subSubDir' contains 'from .subSubA import *'.

# Assumes '__init__.py' is empty in 'subSubDir'
# Adds 'subSubAFun()' and 'subSubAFunTwo()' to the 'subDir' namespace
from .subSubDir.subSubA import *

# Assumes '__init__.py' in 'subSubDir' has 'from .subSubA import *'
# Adds 'subSubAFun()' and 'subSubAFunTwo()' to the 'subDir' namespace
from .subSubDir import *
# __init__.py from subSubDir

# Adds 'subSubAFun()' and 'subSubAFunTwo()' to the 'subSubDir' namespace
from .subSubA import *

# main.py

import subDir

subDir.subAFun() # Hello from subAFun
subDir.subAFunTwo() # Hello from subAFunTwo
subDir.subSubAFun() # Hello from subSubAFun
subDir.subSubAFunTwo() # Hello from subSubAFunTwoCopy the code

Expression and control flow

To choice

Python uses if, elif, and else for basic conditional selection operations:

if x < 0:
     x = 0
     print('Negative changed to zero')
 elif x == 0:
     print('Zero')
 else:
     print('More')Copy the code

Python also supports ternary conditional Operators:

a if condition else bCopy the code

You can also use a Tuple to achieve a similar effect:

# test needs to return True or False
(falseValue, trueValue)[test]

It is safer to enforce judgment
(falseValue, trueValue)[test == True]

Or use the bool conversion function
(falseValue, trueValue)[bool(<expression>)]Copy the code

To iterate over

For-in can be used to iterate through arrays and dictionaries:

words = ['cat'.'window'.'defenestrate']

for w in words:
    print(w, len(w))

Use the array access operator to quickly generate a copy of the array
for w in words[:]:
    if len(w) > 6:
        words.insert(0, w)

# words -> ['defenestrate', 'cat', 'window', 'defenestrate']Copy the code

If we want to traverse a sequence of numbers, we can use Python’s built-in range function:

a = ['Mary'.'had'.'a'.'little'.'lamb']

for i in range(len(a)):
    print(i, a[i])Copy the code

Basic data types

You can use the built-in function to cast:

int(str)
float(str)
str(int)
str(float)Copy the code

Number: indicates the value type

x = 3
print type(x) # Prints "<type 'int'>"
print x       # Prints "3"
print x + 1   # Addition; prints "4"
print x - 1   # Subtraction; prints "2"
print x * 2   # Multiplication; prints "6"
print x ** 2  # Exponentiation; prints "9"
x += 1
print x  # Prints "4"
x *= 2
print x  # Prints "8"
y = 2.5
print type(y) # Prints "<type 'float'>"
print y, y + 1, y * 2, y ** 2 # Prints "2.5 3.5 5.0 6.25"Copy the code

Boolean type

Python provides a common logical operators, but it’s important to note that Python does not use &&, | |, etc., but the direct use of English words.

t = True
f = False
print type(t) # Prints "<type 'bool'>"
print t and f # Logical AND; prints "False"
print t or f  # Logical OR; prints "True"
print not t   # Logical NOT; prints "False"
printt ! = f# Logical XOR; prints "True"Copy the code

String: a String

Python 2 supports Ascii STR (), a separate Unicode () type, and no byte; The default string in Python 3 is utF-8 and contains both byte and bytearray:

type("Guido") # string type is str in python2
# <type 'str'>

Demote to Unicode using the module provided in __future__
from __future__ import unicode_literals
type("Guido") # string type become unicode
# <type 'unicode'>Copy the code

Python strings support sharding, template strings, and other common operations:

var1 = 'Hello World! '
var2 = "Python Programming"

print "var1[0]: ", var1[0]
print "var2[1:5]: ", var2[1:5]
# var1[0]: H
# var2[1:5]: ytho

print "My name is %s and weight is %d kg!" % ('Zara'.21)
# My name is Zara and weight is 21 kg!Copy the code
str[0:4]
len(str)

string.replace("-"."")
",".join(list)
"hi {0}".format('j')
str.find(",")
str.index(",")   # same, but raises IndexError
str.count(",")
str.split(",")

str.lower()
str.upper()
str.title()

str.lstrip()
str.rstrip()
str.strip()

str.islower()Copy the code
Remove all special characters
re.sub('[^A-Za-z0-9]+'.' ', mystring)Copy the code

If you need to determine whether a substring is included or search for a substring:

The # in operator determines the string
if "blah" not in somestring: 
    continue

# find searches for subscripts
s = "This be a string"
if s.find("is") = =- 1:
    print "No 'is' here!"
else:
    print "Found 'is' in the string."Copy the code

Regex: indicates a regular expression

import re

# check whether a match is made
re.match(r'^[aeiou]', str)

# Replace the contents of the string with the character specified by the second argument
re.sub(r'^[aeiou]'.'? ', str)
re.sub(r'(xyz)'.r'\1', str)

Compile to generate a separate regular expression object
expr = re.compile(r'^... $')
expr.match(...)
expr.sub(...)Copy the code

Common expressions are listed below:

Check whether it is an HTML tag
re.search([^ '< / a >] [^ >] * >'.'<a href="#label">')

Common user names and passwords
re.match('^ [a zA - Z0-9 - _] 3 dec} {$'.'Foo') is not None
re.match('^ \ w | [- _] 3 dec} {$'.'Foo') is not None

# Email
re.match('^ ([a - z0-9 _ \. -] +) @ ([\ \. Da - z -] +) \. ([a-z \.] {2, 6}) $'.'[email protected]')

# Url
exp = re.compile(r'''^(https? : \ \ /)? # match HTTP or HTTPS (/ \ \. Da - z - +) # match domain \. ([a-z \.] {2, 6}) # match domain (/ \ \ / \ w - *) \ /? $ # match api or file ''', re.X)
exp.match('www.google.com')

# the IP address
exp = re.compile(r'''^(? : (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [1]? [0-9] [0-9]?) \.) {3} (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [1]? [0-9] [0-9]?) $' ' ', re.X)
exp.match('192.168.1.1')Copy the code

Collection types

List the List:

Operation: Creates additions and deletions

List is the basic sequence type:

l = []
l = list()

Use the string split method to convert a string to a list
str.split(".")

If you want to assemble an array into a string, you can use join
list1 = ['1'.'2'.'3']
str1 = ' '.join(list1)

If it is a numeric array, it needs to be converted first
list1 = [1.2.3]
str1 = ' '.join(str(e) for e in list1)Copy the code

You can use Append and EXTEND to insert elements or concatenate arrays

x = [1.2.3]

x.append([4.5]) # [1, 2, 3, [4, 5]]

x.extend([4.5]) # [1, 2, 3, 4, 5] note that extend returns NoneCopy the code

You can remove elements from a list using pop, slices, del, remove, and so on:

myList = [10.20.30.40.50]

# pop the second element
myList.pop(1) # 20
# myList: myList.pop(1)

# If no arguments are added, the last element pops up by default
myList.pop()

# Use slices to delete elements
a = [  1.2.3.4.5.6 ]
index = 3 # Only Positive index
a = a[:index] + a[index+1 :]

Delete elements by subscript
myList = [10.20.30.40.50]
rmovIndxNo = 3
del myList[rmovIndxNo] # myList: [10, 20, 30, 50]

Remove the element directly using the remove method
letters = ["a"."b"."c"."d"."e"]
numbers.remove(numbers[1])
print(*letters) # used a * to make it unpack you don't have toCopy the code

Iteration: Index Iteration

You can use the basic for loop to iterate over the elements of a list, as in this example:

animals = ['cat'.'dog'.'monkey']
for animal in animals:
    print animal
# Prints "cat", "dog", "monkey", each on its own line.Copy the code

If you want to get the index of the current element while looping, use the enumerate function:

animals = ['cat'.'dog'.'monkey']
for idx, animal in enumerate(animals):
    print '#%d: %s' % (idx + 1, animal)
# Prints "#1: cat", "#2: dog", "#3: monkey", each on its own lineCopy the code

Python also supports Slices:

nums = range(5)    # range is a built-in function that creates a list of integers
print nums         # Prints "[0, 1, 2, 3, 4]"
print nums[2:4]    # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print nums[2:]     # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print nums[:2]     # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
print nums[:]      # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
print nums[:- 1]    # Slice indices can be negative; prints ["0, 1, 2, 3]"
nums[2:4] = [8.9] # Assign a new sublist to a slice
print nums         # Prints "[0, 1, 8, 9, 4]"Copy the code

Comprehensions: transform

Python also uses map, reduce, and filter. Map is used to transform arrays:

Use map to square each element in the array
items = [1.2.3.4.5]
squared = list(map(lambda x: x**2, items))

# map supports functions to be joined as arrays
def multiply(x):
    return (x*x)
def add(x):
    return (x+x)

funcs = [multiply, add]
for i in range(5):
    value = list(map(lambda x: x(i), funcs))
    print(value)Copy the code

Reduce is used for inductive calculation:

# reduce generalizes the values in the array

from functools import reduce
product = reduce((lambda x, y: x * y), [1.2.3.4])

# Output: 24Copy the code

Filter can filter an array:

number_list = range(- 5.5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)

# Output: [-5, -4, -3, -2, -1]Copy the code

A dictionary type

Create add or delete

d = {'cat': 'cute'.'dog': 'furry'}  Create a new dictionary
print d['cat']       The dictionary does not support the Dot operatorCopy the code

If you need to merge two or more dictionary types:

# python 3.5
z = {**x, **y}

# python 2.7
def merge_dicts(*dict_args):
    """ Given any number of dicts, shallow copy and merge into a new dict, precedence goes to key value pairs in latter dicts. """
    result = {}
    for dictionary in dict_args:
        result.update(dictionary)
    return resultCopy the code

The index traversal

Element access can be made directly by key:

# Python will raise KeyError for accessing non-existent keys, requiring either a check or get
print 'cat' in d     # Check if a dictionary has a given key; prints "True"

If [] is used to select the value, you must first check the existence of the key, otherwise an exception will be raised
print d['monkey']  # KeyError: 'monkey' not a key of d

Use the get function to set the default value
print d.get('monkey'.'N/A')  # Get an element with a default; prints "N/A"
print d.get('fish'.'N/A')    # Get an element with a default; prints "wet"


d.keys() Use keys to retrieve all keysCopy the code

You can use for-in to iterate over groups of numbers:

# traversal keys
for key in d:

# Slower than the previous method
for k in dict.keys(): ...

# Directly iterate over values
for value in dict.itervalues(): ...

# key traversal in Python 2.x
for key, value in d.iteritems():

# key traversal in Python 3.x
for key, value in d.items():Copy the code

Other sequence types

A collection of

# Same as {"a", "b","c"}
normal_set = set(["a"."b"."c"])

# Adding an element to normal set is fine
normal_set.add("d")

print("Normal Set")
print(normal_set)

# A frozen set
frozen_set = frozenset(["e"."f"."g"])

print("Frozen Set")
print(frozen_set)

# Uncommenting below line would cause error as
# we are trying to add element to a frozen set
# frozen_set.add("h")Copy the code

function

The function definitions

Functions in Python are defined using the def keyword, for example:

def sign(x):
    if x > 0:
        return 'positive'
    elif x < 0:
        return 'negative'
    else:
        return 'zero'


for x in [- 1.0.1] :print sign(x)
# Prints "negative", "zero", "positive"Copy the code

Python supports runtime creation of dynamic functions, known as lambda functions:

def f(x): return x**2

# is equivalent to
g = lambda x: x**2Copy the code

parameter

Option Arguments: Indefinite Arguments

def example(a, b=None, *args, **kwargs):
  print a, b
  print args
  print kwargs

example(1."var".2.3, word="hello")
# 1 var
# (2, 3)
# {'word': 'hello'}

a_tuple = (1.2.3.4.5)
a_dict = {"1":1."2":2."3":3}
example(1."var", *a_tuple, **a_dict)
# 1 var
# (1, 2, 3, 4, 5)
# {'1': 1, '2': 2, '3': 3}Copy the code

The generator

def simple_generator_function(a):
    yield 1
    yield 2
    yield 3

for value in simple_generator_function():
    print(value)

# output result
# 1
# 2
# 3
our_generator = simple_generator_function()
next(our_generator)
# 1
next(our_generator)
# 2
next(our_generator)
# 3

Typical usage scenarios for generators are such as iterating over infinite arrays
def get_primes(number):
    while True:
        if is_prime(number):
            yield number
        number += 1Copy the code

A decorator

Decorators are very useful design patterns:

# Simple decorator

from functools import wraps
def decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        print('wrap function')
        return func(*args, **kwargs)
    return wrapper

@decorator
def example(*a, **kw):
    pass

example.__name__  # attr of function preserve
# 'example'
# Decorator 

# decorator with input values

from functools import wraps
def decorator_with_argument(val):
  def decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
      print "Val is {0}".format(val)
      return func(*args, **kwargs)
    return wrapper
  return decorator

@decorator_with_argument(10)
def example(a):
  print "This is example function."

example()
# Val is 10
# This is example function.

# is equivalent to

def example(a):
  print "This is example function."

example = decorator_with_argument(10)(example)
example()
# Val is 10
# This is example function.Copy the code

Classes and objects

The class definition

The definition of a class in Python is also straightforward:

class Greeter(object):

    # Constructor
    def __init__(self, name):
        self.name = name  # Create an instance variable

    # Instance method
    def greet(self, loud=False):
        if loud:
            print 'HELLO, %s! ' % self.name.upper()
        else:
            print 'Hello, %s' % self.name

g = Greeter('Fred')  # Construct an instance of the Greeter class
g.greet()            # Call an instance method; prints "Hello, Fred"
g.greet(loud=True)   # Call an instance method; prints "HELLO, FRED!"Copy the code
The # isinstance method is used to determine whether an object is derived from a class
ex = 10
isinstance(ex,int)Copy the code

Managed Attributes: Managed Attributes

Property, setter, deleter can be used to replicate point methods

class Example(object):
    def __init__(self, value):
       self._val = value
    @property
    def val(self):
        return self._val
    @val.setter
    def val(self, value):
        if not isintance(value, int):
            raise TypeError("Expected int")
        self._val = value
    @val.deleter
    def val(self):
        del self._val
    @property
    def square3(self):
        return 2**3

ex = Example(123)
ex.val = "str"
# Traceback (most recent call last):
# File "", line 1, in
# File "test.py", line 12, in val
# raise TypeError("Expected int")
# TypeError: Expected intCopy the code

Class methods versus static methods

class example(object):
  @classmethod
  def clsmethod(cls):
    print "I am classmethod"
  @staticmethod
  def stmethod(a):
    print "I am staticmethod"
  def instmethod(self):
    print "I am instancemethod"

ex = example()
ex.clsmethod()
# I am classmethod
ex.stmethod()
# I am staticmethod
ex.instmethod()
# I am instancemethod
example.clsmethod()
# I am classmethod
example.stmethod()
# I am staticmethod
example.instmethod()
# Traceback (most recent call last):
# File "", line 1, in
# TypeError: unbound method instmethod() ...Copy the code

object

instantiation

Attribute operation

Object attributes in Python, unlike dictionary keys, can be evaluated using the dot operator. Using in directly causes problems:

class A(object):
    @property
    def prop(self):
        return 3

a = A()
print "'prop' in a.__dict__ =".'prop' in a.__dict__
print "hasattr(a, 'prop') =", hasattr(a, 'prop')
print "a.prop =", a.prop

# 'prop' in a.__dict__ = False
# hasattr(a, 'prop') = True
# a.prop = 3Copy the code

It is recommended to use hasattr, getattr, setattr to operate on object attributes:

class Example(object):
  def __init__(self):
    self.name = "ex"
  def printex(self):
    print "This is an example"


# Check object has attributes
# hasattr(obj, 'attr')
ex = Example()
hasattr(ex,"name")
# True
hasattr(ex,"printex")
# True
hasattr(ex,"print")
# False

# Get object attribute
# getattr(obj, 'attr')
getattr(ex,'name')
# 'ex'

# Set object attribute
# setattr(obj, 'attr', value)
setattr(ex,'name'.'example')
ex.name
# 'example'Copy the code

Exceptions and Tests

Exception handling

Context Manager – with

With is often used to open or close certain resources:

host = 'localhost'
port = 5566
with Socket(host, port) as s:
    while True:
        conn, addr = s.accept()
        msg = conn.recv(1024)
        print msg
        conn.send(msg)
        conn.close()Copy the code

Unit testing

from __future__ import print_function

import unittest

def fib(n):
    return 1 if n<=2 else fib(n- 1)+fib(n2 -)

def setUpModule(a):
        print("setup module")
def tearDownModule(a):
        print("teardown module")

class TestFib(unittest.TestCase):

    def setUp(self):
        print("setUp")
        self.n = 10
    def tearDown(self):
        print("tearDown")
        del self.n
    @classmethod
    def setUpClass(cls):
        print("setUpClass")
    @classmethod
    def tearDownClass(cls):
        print("tearDownClass")
    def test_fib_assert_equal(self):
        self.assertEqual(fib(self.n), 55)
    def test_fib_assert_true(self):
        self.assertTrue(fib(self.n) == 55)

if __name__ == "__main__":
    unittest.main()Copy the code

storage

File to read and write

Path to deal with

Python’s built-in __file__ keyword points to the relative path of the current file, which can be used to construct absolute paths, or to index other files:

Get the relative directory of the current file
dir = os.path.dirname(__file__) # src\app

## once you're at the directory level you want, with the desired directory as the final path node:
dirname1 = os.path.basename(dir) 
dirname2 = os.path.split(dir)[1] ## if you look at the documentation, this is exactly what os.path.basename does.

Abspath will automatically complete the path based on the relative path and the current workspace
os.path.abspath(os.path.dirname(__file__)) # D:\WorkSpace\OWS\tool\ui-tool-svn\python\src\app

Get the real path of the current file
os.path.dirname(os.path.realpath(__file__)) # D:\WorkSpace\OWS\tool\ui-tool-svn\python\src\app

Get the current execution path
os.getcwd()Copy the code

You can use the listdir, Walk, glob modules to enumerate and retrieve files:

Just list all files
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

Use walk to search recursively
from os import walk

f = []
for (dirpath, dirnames, filenames) in walk(mypath):
    f.extend(filenames)
    break

Use glob for complex pattern matching
import glob
print(glob.glob("/home/adam/*.txt"))
# ['/home/adam/file1.txt', '/home/adam/file2.txt', .... ]Copy the code

Simple file read and write

Write mode can be selected based on whether the file exists
mode = 'a' if os.path.exists(writepath) else 'w'

Use the with method to handle exceptions automatically
with open("file.dat",mode) asf: f.write(...) .Remember to close the file when you're done
    f.close()

Read the contents of the file
message = f.read()Copy the code

Complex format file

JSON

import json

# Writing JSON data
with open('data.json'.'w') as f:
     json.dump(data, f)

# Reading data back
with open('data.json'.'r') as f:
     data = json.load(f)Copy the code

XML

LXML can be used to parse and process XML files, and this section introduces common operations. LXML supports creating Element objects from strings or files:

from lxml import etree

Can be constructed from a string
xml = '<a xmlns="test"><b xmlns="test"/></a>'
root = etree.fromstring(xml)
etree.tostring(root)
# b'<a xmlns="test"><b xmlns="test"/></a>'

You can also build from a file
tree = etree.parse("doc/test.xml")

Or specify a baseURL
root = etree.fromstring(xml, base_url="http://where.it/is/from.xml")Copy the code

Iterators are provided to iterate over all elements:

Pass through all nodes
for tag in tree.iter():
    if not len(tag):
        print tag.keys() Get all custom attributes
        print (tag.tag, tag.text) # text is the text child value

# get XPath
for e in root.iter():
    print tree.getpath(e)Copy the code

LXML supports XPath look-ups of elements, though it is important to note that XPath look-ups result in arrays, and namespaces need to be specified in cases where namespaces are included:

root.xpath('//page/text/text()',ns={prefix:url})

You can use getparent to recursively find the parent element
el.getparent()Copy the code

LXML provides insert, append, and other methods for manipulating elements:

The # append method is appended to the end by default
st = etree.Element("state", name="New Mexico")
co = etree.Element("county", name="Socorro")
st.append(co)

The # insert method can specify the position
node.insert(0, newKid)Copy the code

Excel

You can use XLRD to read Excel files and XlsxWriter to write and manipulate Excel files.

Read the original value of a Cell
sh.cell(rx, col).valueCopy the code
Create a new file
workbook = xlsxwriter.Workbook(outputFile)
worksheet = workbook.add_worksheet()

# setting is written from line 0
row = 0

Iterate over a two-dimensional array and write it to Excel
for rowData in array:
    for col, data in enumerate(rowData):
        worksheet.write(row, col, data)
    row = row + 1

workbook.close()Copy the code

The file system

For advanced file manipulation, we can use Shutil built into Python


Delete all folders under appName recursively
shutil.rmtree(appName)Copy the code

Network interaction

Requests

Requests is the elegant and easy-to-use Python network request library:

import requests

r = requests.get('https://api.github.com/events')
r = requests.get('https://api.github.com/user', auth=('user'.'pass'))

r.status_code
# 200
r.headers['content-type']
# 'application/json; charset=utf8'
r.encoding
# 'utf-8'
r.text
# u'{"type":"User"... '
r.json()
# {u'private_gists': 419, u'total_private_repos': 77, ... }

r = requests.put('http://httpbin.org/put', data = {'key':'value'})
r = requests.delete('http://httpbin.org/delete')
r = requests.head('http://httpbin.org/get')
r = requests.options('http://httpbin.org/get')Copy the code

Data is stored

MySQL

import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             db='db',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

try:
    with connection.cursor() as cursor:
        # Create a new record
        sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
        cursor.execute(sql, ('[email protected]'.'very-secret'))

    # connection is not autocommit by default. So you must commit to save
    # your changes.
    connection.commit()

    with connection.cursor() as cursor:
        # Read a single record
        sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
        cursor.execute(sql, ('[email protected]',))
        result = cursor.fetchone()
        print(result)
finally:
    connection.close()Copy the code