python:包和模块(python3)
一、包和模块的说明(python3)
1、包: 包,就是存放‘模块’的‘文件夹’;‘包的名字’,就是‘此文件夹的名字’。每个‘包’中都包含文件‘__init__.py’,文件‘__init__.py’表明‘这个文件夹’是‘python的包’。
2、模块: 模块,就是‘python的源文件’。模块的形式,如‘xx.py’。
3、模块的使用:
3.1、 import 包名.包名 ... .包名.模块名
3.2、 from 包名.包名 ... .包名.模块名 import 模块的部分内容 #模块的部分内容,指的是‘模块中的全局变量、函数、类等内容’。
二、模块的编写(包名:libs/test;模块名:testing)
1 wit@on:libs$ pwd
2 /home/wit/user/lidawei/tmp/python/libs
3 wit@on:libs$
4 wit@on:libs$
5 wit@on:libs$ cat ../files/data.json
6 {
7 "id":["20230001","20230002", "2023003"],
8 "name":["laohu", "laoying", "shizi"]
9 }
10 wit@on:libs$
11 wit@on:libs$
12 wit@on:libs$ tree
13 .
14 ├── __init__.py
15 ├── __pycache__
16 │ └── __init__.cpython-310.pyc
17 ├── template
18 │ └── __init__.py
19 └── test
20 ├── __init__.py
21 ├── __pycache__
22 │ ├── __init__.cpython-310.pyc
23 │ └── testing.cpython-310.pyc
24 └── testing.py
25
26 4 directories, 7 files
27 wit@on:libs$
28 wit@on:libs$
29 wit@on:libs$ cat test/testing.py
30 def info():
31 print("This is a testing modules. \n")
32
33 wit@on:libs$
34 wit@on:libs$
三、模块的应用
1 wit@on:libs$ pwd
2 /home/wit/user/lidawei/tmp/python/libs
3 wit@on:libs$
4 wit@on:libs$
5 wit@on:libs$ cat ../files/data.json
6 {
7 "id":["20230001","20230002", "2023003"],
8 "name":["laohu", "laoying", "shizi"]
9 }
10 wit@on:libs$
11 wit@on:libs$
12 wit@on:libs$ tree
13 .
14 ├── __init__.py
15 ├── __pycache__
16 │ └── __init__.cpython-310.pyc
17 ├── template
18 │ └── __init__.py
19 └── test
20 ├── __init__.py
21 ├── __pycache__
22 │ ├── __init__.cpython-310.pyc
23 │ └── testing.cpython-310.pyc
24 └── testing.py
25
26 4 directories, 7 files
27 wit@on:libs$
28 wit@on:libs$
29 wit@on:libs$ cat test/testing.py
30 def info():
31 print("This is a testing modules. \n")
32
33 wit@on:libs$
34 wit@on:libs$
35 wit@on:libs$ cd ..
36 wit@on:python$
37 wit@on:python$
38 wit@on:python$ cat filepy
39 #!/usr/bin/env python3
40
41
42
43 import json
44
45
46
47 file1 = './files/data.json'
48 fh1 = open(file1, "r")
49 db1 = fh1.read()
50 print(f'db1 := {db1}\n')
51
52
53
54 # json part
55 python_data1 = json.loads(db1)
56 print(f'python_data := {python_data1}\n')
57 print(f'python_data["id"][0] := { python_data1["id"][0] }\n')
58 print(f'python_data["name"][0] := { python_data1["name"][0] }\n')
59
60
61
62 fh1.close()
63 wit@on:python$
64 wit@on:python$
65 wit@on:python$ ./filepy
66 db1 := {
67 "id":["20230001","20230002", "2023003"],
68 "name":["laohu", "laoying", "shizi"]
69 }
70
71
72 python_data := {'id': ['20230001', '20230002', '2023003'], 'name': ['laohu', 'laoying', 'shizi']}
73
74 python_data["id"][0] := 20230001
75
76 python_data["name"][0] := laohu
77
78 wit@on:python$
79 wit@on:python$
四、参考资料:
1、python3模块|菜鸟教程: https://www.runoob.com/python3/python3-module.html
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/17348267.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2020-04-24 win10_64_ssh连接linux
2020-04-24 win10防火墙服务灰色解决办法