当前位置: 首页 > 工具软件 > ini-parser > 使用案例 >

【Python的configparser模块读取.ini文件内容并输出】

黎承颜
2023-12-01

configparser是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近。Python2.x 中名为 ConfigParser,3.x 已更名小写。下文通过使用python的configparser模块实现解析.ini配置文件并输出的功能。

1.编辑.ini文件

[xmaster@mogdb-kernel-0005 python]$ cat test.ini 
[ysl]
key1 = 2022
key2 = 1017

2.编写python脚本

[xmaster@mogdb-kernel-0005 python]$ cat 1.py 
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import configparser
cfp = configparser.ConfigParser()
cfp.read("test.ini")

'''获取所有的selections'''
selections = cfp.sections()
print(selections)

'''获取指定selections下的所有options'''
options = cfp.options("ysl")
print(options)

'''获取指定selection下的指定option的值'''
value= cfp.get("ysl", "key1")
print(value)

'''判断是否含有指定selection 或 option'''
has_sec=cfp.has_section("ysl")
print(has_sec)
has_opt=cfp.has_option("ysl",'key3')
print(has_opt)

3.执行效果

[xmaster@mogdb-kernel-0005 python]$ python 1.py 
['ysl']
['key1', 'key2']
2022
True
False

4.把python脚本打包成可执行文件
需要安装pip和pyinstaller

[root@mogdb-kernel-0005 ~]# pip3 --version
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)

[root@mogdb-kernel-0005 ~]# pip3 install pyinstaller
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting pyinstaller
  Downloading https://files.pythonhosted.org/packages/b0/e6/e5760666896739115b0e4538a42cdd895215581618ec885ad043dd35ee57/pyinstaller-4.10.tar.gz (2.7MB)
    100% |████████████████████████████████| 2.7MB 50kB/s 
Requirement already satisfied: setuptools in /usr/lib/python3.6/site-packages (from pyinstaller)
Collecting altgraph (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/cc/ff/88d277ba936d226b0f6dbd6711145f90fcfeed3aa9455c1c2f62c8ffec5b/altgraph-0.17.3-py2.py3-none-any.whl
Collecting pyinstaller-hooks-contrib>=2020.6 (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/a5/51/aca8508043b9564620bdd96adbc824b8d84537afcfb92ff806ea119ee176/pyinstaller_hooks_contrib-2022.0-py2.py3-none-any.whl (222kB)
    100% |████████████████████████████████| 225kB 33kB/s 
Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.6/site-packages (from pyinstaller)
Requirement already satisfied: typing-extensions>=3.6.4; python_version < "3.8" in /usr/local/lib/python3.6/site-packages (from importlib-metadata->pyinstaller)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.6/site-packages (from importlib-metadata->pyinstaller)
Building wheels for collected packages: pyinstaller
  Running setup.py bdist_wheel for pyinstaller ... done
  Stored in directory: /root/.cache/pip/wheels/a2/e3/33/c95b38f0c64fa9bfaaef81263a1efb453a8ee946f8febcc79d
Successfully built pyinstaller
Installing collected packages: altgraph, pyinstaller-hooks-contrib, pyinstaller
Successfully installed altgraph-0.17.3 pyinstaller-4.10 pyinstaller-hooks-contrib-2022.0

打包python脚本

[xmaster@mogdb-kernel-0005 python]$ pyinstaller -F 1.py 
61 INFO: PyInstaller: 4.10
61 INFO: Python: 3.6.8
61 INFO: Platform: Linux-4.18.0-348.7.1.el8_5.x86_64-x86_64-with-centos-8.0.1905-Core
62 INFO: wrote /home/xmaster/python/1.spec
63 INFO: UPX is not available.
64 INFO: Extending PYTHONPATH with paths
['/home/xmaster/python']
205 INFO: checking Analysis
205 INFO: Building Analysis because Analysis-00.toc is non existent
205 INFO: Initializing module dependency graph...
206 INFO: Caching module graph hooks...
213 INFO: Analyzing base_library.zip ...
2717 INFO: Caching module dependency graph...
2842 INFO: running Analysis Analysis-00.toc
2866 INFO: Analyzing /home/xmaster/python/1.py
2868 INFO: Processing module hooks...
2869 INFO: Loading module hook 'hook-encodings.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
2914 INFO: Loading module hook 'hook-heapq.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
2915 INFO: Loading module hook 'hook-pickle.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
2916 INFO: Loading module hook 'hook-difflib.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
2917 INFO: Loading module hook 'hook-xml.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
3125 INFO: Looking for ctypes DLLs
3125 INFO: Analyzing run-time hooks ...
3127 INFO: Including run-time hook '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks/rthooks/pyi_rth_subprocess.py'
3128 INFO: Including run-time hook '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks/rthooks/pyi_rth_pkgutil.py'
3130 INFO: Including run-time hook '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks/rthooks/pyi_rth_inspect.py'
3133 INFO: Looking for dynamic libraries
3367 INFO: Looking for eggs
3367 INFO: Using Python library /lib64/libpython3.6m.so.1.0
3370 INFO: Warnings written to /home/xmaster/python/build/1/warn-1.txt
3386 INFO: Graph cross-reference written to /home/xmaster/python/build/1/xref-1.html
3398 INFO: checking PYZ
3398 INFO: Building PYZ because PYZ-00.toc is non existent
3398 INFO: Building PYZ (ZlibArchive) /home/xmaster/python/build/1/PYZ-00.pyz
3779 INFO: Building PYZ (ZlibArchive) /home/xmaster/python/build/1/PYZ-00.pyz completed successfully.
3781 INFO: checking PKG
3781 INFO: Building PKG because PKG-00.toc is non existent
3781 INFO: Building PKG (CArchive) 1.pkg
6090 INFO: Building PKG (CArchive) 1.pkg completed successfully.
6092 INFO: Bootloader /usr/local/lib/python3.6/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
6092 INFO: checking EXE
6092 INFO: Building EXE because EXE-00.toc is non existent
6092 INFO: Building EXE from EXE-00.toc
6092 INFO: Copying bootloader EXE to /home/xmaster/python/dist/1
6092 INFO: Appending PKG archive to custom ELF section in EXE
6125 INFO: Building EXE from EXE-00.toc completed successfully.

可执行文件在dist目录下,直接执行报了个错,是因为他读取的test.ini文件目录不对,这里不修改代码直接把test.ini拷贝到同级目录下。

[xmaster@mogdb-kernel-0005 python]$ ls
1.py  1.spec  build  dist  __pycache__  test.ini
[xmaster@mogdb-kernel-0005 python]$ cd dist/
[xmaster@mogdb-kernel-0005 dist]$ ls
1
[xmaster@mogdb-kernel-0005 dist]$ ./1
[]
Traceback (most recent call last):
  File "1.py", line 12, in <module>
  File "configparser.py", line 675, in options
configparser.NoSectionError: No section: 'ysl'
[240600] Failed to execute script '1' due to unhandled exception!

[xmaster@mogdb-kernel-0005 dist]$ cp ../test.ini ./
[xmaster@mogdb-kernel-0005 dist]$ ./1
['ysl']
['key1', 'key2']
2022
True
False

 类似资料: