Python的configparser模組讀取.ini檔案內容並輸出
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Java讀取本地檔案內容並輸出Java
- Python 使用ConfigParser操作ini配置檔案教程。Python
- 透過python讀取ini配置檔案Python
- Python進階,ConfigParser:Python中對於ini格式的配置檔案的使用Python
- java檔案相關(檔案追加內容、檔案內容清空、檔案內容讀取)Java
- python模組之configparserPython
- go 讀取.ini配置檔案Go
- mybatis讀取properties檔案內容MyBatis
- Python學習——configparser模組Python
- 【自動化測試】Python 讀取 .ini 格式檔案Python
- python解壓並讀取檔案Python
- 用python寫一個指令碼,讀取srt檔案中的內容,並列印出重複的內容,且將不重複的內容儲存到新檔案中Python指令碼
- 教你一招!Python讀取檔案內容為字串的方法Python字串
- python中按照資料夾中檔案的排列順序讀取檔案內容Python
- vite vue-cli 讀取檔案原始內容 使用base64內容的檔案ViteVue
- LIUNUX如何擷取txt檔案中的內容,並建立新檔案UX
- python讀取文字檔案內容的方法主要分為哪三種?Python
- python常用模組補充hashlib configparser logging,subprocess模組Python
- 讀取resources中properties檔案內容範例
- 用 ABAP 讀取本地文字檔案內容試讀版
- 教大家python讀取一行一行檔案內容的方法Python
- cat-合併輸出多個檔案的內容
- python包和模組的內容整理Python
- JavaScript讀取文字檔案內容程式碼例項JavaScript
- 讀取.net core配置檔案appsetting.json內容APPJSON
- MATLAB|讀取一個檔案,並將其不同內容儲存為不同的矩陣Matlab矩陣
- Python中Spark讀取parquet檔案並獲取schema的JSON表示PythonSparkJSON
- 使用Python讀取PlantUML匯出的XMI檔案Python
- python讀取兩個excel資料檔案輸出整理好以後的excel資料檔案PythonExcel
- JavaScript讀取本地檔案內容(相容低版本IE)JavaScript
- python:檔案的輸入與輸出Python
- 如何使用Python讀取文字檔案並回答問題?Python
- 怎麼透過Python獲取檔案指定行的內容?Python
- java 讀寫 ini 配置檔案Java
- ENVI匯出的tif檔案,python讀取失敗。Python
- 讀取檔案流並寫入檔案流
- php獲取xml檔案內容PHPXML
- Python常用配置檔案ini、json、yaml讀寫總結PythonJSONYAML