current position:Home>Glom module of Python data analysis module (1)
Glom module of Python data analysis module (1)
2022-01-30 13:48:33 【cxapython】
Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .
This article has participated in 「 Digging force Star Program 」 , Win a creative gift bag , Challenge creation incentive fund
Under the simple said glom The module is mainly used for processing structured data , Simple installation pip install glom that will do , Next is glom Explain the method parameters with examples .
glom
With the same name as the module glom Method of use :
.glom(target, spec, **kwargs)
target Parameters are structured data , It's usually json Nested Type .
spec Parameters are defined templates
**kwargs Contains parameters of key value type :default,skip_exc,scope
Let's take a simple example 、
target = {'a': {'b':{'c':{'d':{'e':[1,2,3,4,5,6,7]}}}}}
spec = 'a.b.c.d.e'
output = glom(target, spec)
print(output)
# Output [1, 2, 3, 4, 5, 6, 7]
Copy code
Let's try more games :
target = {'a': {'b':{'c':{'d':{'e':[1,2,3,4,5,6,7]}}}}}
spec = {'aa':'a.b.c.d.e'}
output = glom(target, spec)
print(output)
# Output {'aa': [1, 2, 3, 4, 5, 6, 7]}
target2 = {'a': {'b':{'c':[{'d':[1,2,3,4]},{'d':[4,5,6,7]}]}}}
spec2 = {'test':('a.b.c',['d'])}
output2=glom(target2, spec2)
print(output2)
# Output {'test': [[1, 2, 3, 4], [4, 5, 6, 7]]}
Copy code
Another official example , Make comprehensive use of :
target = {'system': {'planets': [{'name': 'earth', 'moons': 1},
{'name': 'jupiter', 'moons': 69}]}}
spec = {'names': ('system.planets', ['name']),
'moons': ('system.planets', ['moons'])}
Output :{'moons': [1, 69], 'names': ['earth', 'jupiter']}
Copy code
As can be seen from the above examples ,spec The basic usage is '' Write the key names in turn . Key name . Key name . Key name ,
Another thing to note , If there is a dictionary in the list to take its value, the key name must be the same, just above 'd' key .
.
It can be seen that glom and json It saves a lot of []. Easy to use , in addition spec The expression also supports the use of expressions, such as lambda.
Let's take a look at an example with complete parameters
target = {'a': [0, 1, 2]} # Given a structured data
spec = {'a': ('a', [lambda x:x/x])}# obtain a The value of each element divided by its own value .
output = glom(target, spec,default='666',skip_exc=ZeroDivisionError)
print(output)
# Output 666
Copy code
The above results mainly demonstrate spec The advanced usage of and the usage of the third parameter . The above calculation 0/0 It's a mistake , Wrong type ZeroDivisionError, So we let ZeroDivisionError Error skipped , Simultaneous coordination default The default value when given an error .
But what if the following happens ?
t={'a': {'b.c':{'c':{'d':{'e':[1,2,3,4,5,6,7]}}}}}
s= {'aa':'a.b.c.c'}
print(glom(t,s))
Copy code
Now let's see Path Method
Path
glom.Path(*path_parts)
Path objects specify explicit paths when the default 'a.b.c'-style general access syntax won’t work or isn’t desirable. Use this to wrap ints, datetimes, and other valid keys, as well as strings with dots that shouldn’t be expanded, Let's look at an example .
from glom import glom,Path
target2 = {'a': {'b':{'c':[{'d':[1,2,3,4]},{'d':[4,5,6,7]}],'w':{'3.c':'e'}}}}
output2_1=Path(glom(target2,Path('a','b','w','3.c')))
print(output2_1)
# Output e
Copy code
Then let's go back and look at the use of spec Errors in the way
t={'a': {'b.c':{'c':{'d':{'e':[1,2,3,4,5,6,7]}}}}}
print(glom(t,{'aa':Path('a','b.c','c')}))
# Output {'aa': {'d': {'e': [1, 2, 3, 4, 5, 6, 7]}}}
Copy code
Be careful :
1.path The hierarchical relationship of the path must be next to . For example, I directly Path('a','c') It's going to be a mistake .
2.path You can't take the keys in the list like this. You need to cooperate glom Handle .
Path('system','planets','name') perhaps Path('system','planets',['name']) Neither. .
target = {'system': {'planets': [{'name': 'earth', 'moons': 1},
{'name': 'jupiter', 'moons': 69}]}}
follow-up ...
Copy code
copyright notice
author[cxapython],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301348301701.html
The sidebar is recommended
- Python code reading (Part 44): find the location of qualified elements
- Elegant implementation of Django model field encryption
- 40 Python entry applet
- Pandas comprehensive application
- Chapter 2: Fundamentals of python-3 character string
- Python pyplot draws a parallel histogram, and the x-axis value is displayed in the center of the two histograms
- [Python crawler] detailed explanation of selenium from introduction to actual combat [1]
- Curl to Python self use version
- Python visualization - 3D drawing solutions pyecharts, Matplotlib, openpyxl
- Use python, opencv's meanshift and CAMSHIFT algorithms to find and track objects in video
guess what you like
-
Using python, opencv obtains and changes pixels, modifies image channels, and trims ROI
-
[Python data collection] university ranking data collection
-
[Python data collection] stock information collection
-
Python game development, pyGame module, python takes you to realize a magic tower game from scratch (2)
-
Python solves the problem of suspending execution after clicking the mouse in CMD window (fast editing mode is prohibited)
-
[Python from introduction to mastery] (II) how to run Python? What are the good development tools (pycharm)
-
Python type hints from introduction to practice
-
Python notes (IX): basic operation of dictionary
-
Python notes (8): basic operations of collections
-
Python notes (VII): definition and use of tuples
Random recommended
- Python notes (6): definition and use of lists
- Python notes (V): string operation
- Python notes (IV): use of functions and modules
- Python notes (3): conditional statements and circular statements
- Python notes (II): lexical structure
- Notes on python (I): getting to know Python
- [Python data structure series] - tree and binary tree - basic knowledge - knowledge point explanation + code implementation
- [Python daily homework] Day7: how to combine two dictionaries in an expression?
- How to implement a custom list or dictionary in Python
- 15 advanced Python tips for experienced programmers
- Python string method tutorial - how to use the find() and replace() functions on Python strings
- Python computer network basics
- Python crawler series: crawling global airport information
- Python crawler series: crawling global port information
- How to calculate unique values using pandas groupby
- Application of built-in distribution of Monte Carlo simulation SciPy with Python
- Gradient lifting method and its implementation in Python
- Pandas: how to group and calculate by index
- Can you create an empty pandas data frame and fill it in?
- Python basic exercises teaching! can't? (practice makes perfect)
- Exploratory data analysis (EDA) in Python using SQL and Seaborn (SNS).
- Turn audio into shareable video with Python and ffmpeg
- Using rbind in python (equivalent to R)
- Pandas: how to create an empty data frame with column names
- Talk about quantifying investment using Python
- Python, image restoration in opencv - CV2 inpaint
- Python notes (14): advanced technologies such as object-oriented programming
- Python notes (13): operations such as object-oriented programming
- Python notes (12): inheritance such as object-oriented programming
- Chapter 2: Fundamentals of python-5 Boolean
- Python notes (11): encapsulation such as object-oriented programming
- Python notes (10): concepts such as object-oriented programming
- Gradient lifting method and its implementation in Python
- Van * Python | simple crawling of a site course
- Chapter 1 preliminary knowledge of pandas (list derivation and conditional assignment, anonymous function and map method, zip object and enumerate method, NP basis)
- Nanny tutorial! Build VIM into an IDE (Python)
- Fourier transform of Python OpenCV image processing, lesson 52
- Introduction to python (III) network request and analysis
- China Merchants Bank credit card number recognition project (Part I), python OpenCV image processing journey, Part 53
- Python practice - capture 58 rental information and store it in MySQL database