Python JSON 格式 & 內容比對
2014-08-20
Kommentare
Type Check
Source :
Python - Determine the type of an object?
What’s the canonical way to check for type in python?
>>> type([]) is list
True
>>> type({}) is dict
True
>>> type('') is str
True
>>> type(0) is int
True
>>> isinstance(b, Test1)
True
>>> isinstance(b, Test2)
True
>>> isinstance(a, Test1)
True
>>> isinstance(a, Test2)
False
>>> isinstance([], list)
True
>>> isinstance({}, dict)
True
JSON Diff
別人做好的JSON比較(網頁版)
Link : JSON Diff
XML To dict
Source :
How to convert XML to JSON in Python? [duplicate]
Install :
pip install xmltodict
Usage :
import xmltodict, json
o = xmltodict.parse('<e> <a>text</a> <a>text</a> </e>')
json.dumps(o) # '{"e": {"a": ["text", "text"]}}'
Python 建構子
class MySuperClass:
def __init__(self,url,paths = []):
pass
class MySubClass(MySuperClass):
def __init__(self,url,paths = [],keysForSearch = []):
MySuperClass.__init__(self,url,paths)
List Comprehensions
Source: Python: List Comprehensions
S = {x² : x in {0 ... 9}}
V = (1, 2, 4, 8, ..., 2¹²)
M = {x | x in S and x even}
>>> S = [x**2 for x in range(10)]
>>> V = [2**i for i in range(13)]
>>> M = [x for x in S if x % 2 == 0]
>>>
>>> print S; print V; print M
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]
[0, 4, 16, 36, 64]
re (isFloatingNumber?)
Source: Regular-Expressions Info
pattern = r"[-+]?[0-9]*\.?[0-9]+"