Python的configparser模組讀取.ini檔案內容並輸出

T1YSL發表於2022-10-21

configparser是 Pyhton 標準庫中用來解析配置檔案的模組,並且內建方法和字典非常接近。Python2.x 中名為 ConfigParser,3.x 已更名小寫。下文透過使用python的configparser模組實現解析.ini配置檔案並輸出的功能。

1.編輯.ini檔案

[xmaster@mogdb-kernel-0005 python]$ cat test.ini 
[ysl]key1 = 2022key2 = 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']2022TrueFalse

4.把python指令碼打包成可執行檔案
需要安裝pip和pyinstaller

[root@mogdb-kernel-0005 ~]# pip3 --versionpip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
[root@mogdb-kernel-0005 ~]# pip3 install pyinstallerWARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting pyinstaller
  Downloading (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 
Collecting pyinstaller-hooks-contrib>=2020.6 (from pyinstaller)
  Downloading (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.1061 INFO: Python: 3.6.861 INFO: Platform: Linux-4.18.0-348.7.1.el8_5.x86_64-x86_64-with-centos-8.0.1905-Core62 INFO: wrote /home/xmaster/python/1.spec63 INFO: UPX is not available.64 INFO: Extending PYTHONPATH with paths
['/home/xmaster/python']205 INFO: checking Analysis205 INFO: Building Analysis because Analysis-00.toc is non existent205 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.toc2866 INFO: Analyzing /home/xmaster/python/1.py2868 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 DLLs3125 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 libraries3367 INFO: Looking for eggs3367 INFO: Using Python library /lib64/libpython3.6m.so.1.03370 INFO: Warnings written to /home/xmaster/python/build/1/warn-1.txt3386 INFO: Graph cross-reference written to /home/xmaster/python/build/1/xref-1.html3398 INFO: checking PYZ3398 INFO: Building PYZ because PYZ-00.toc is non existent3398 INFO: Building PYZ (ZlibArchive) /home/xmaster/python/build/1/PYZ-00.pyz3779 INFO: Building PYZ (ZlibArchive) /home/xmaster/python/build/1/PYZ-00.pyz completed successfully.3781 INFO: checking PKG3781 INFO: Building PKG because PKG-00.toc is non existent3781 INFO: Building PKG (CArchive) 1.pkg6090 INFO: Building PKG (CArchive) 1.pkg completed successfully.6092 INFO: Bootloader /usr/local/lib/python3.6/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run6092 INFO: checking EXE6092 INFO: Building EXE because EXE-00.toc is non existent6092 INFO: Building EXE from EXE-00.toc6092 INFO: Copying bootloader EXE to /home/xmaster/python/dist/16092 INFO: Appending PKG archive to custom ELF section in EXE6125 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


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69990629/viewspace-2919711/,如需轉載,請註明出處,否則將追究法律責任。

相關文章