Reading configuration file ini of python

Follow the main line of developing thin client connecting remote desktop GUI program in raspberry pie, touch here, use pyqt5 to develop graphical interface, the program reads an ini configuration file, and writes the relevant parameters of remote connection to the INI configuration file. In this way, you can implement a simplest version of the remote desktop connection program.

Above is the background, only one record is made.

This article refers to this website:

    https://blog.csdn.net/songlh1234/article/details/83316468


I. read configuration file

There is a config.ini configuration file under config

#Define config group
[config]
platformName=Android
appPackage=com.romwe
appActivity=com.romwe.SplashActivity
 
#Define cmd group
[cmd]
viewPhone=adb devices
startServer=adb start-server
stopServer=adb kill-server
install=adb install aaa.apk
id=1
weight=12.1
isChoice=True
 
#Define log group
[log]
log_error=true


Write the script code to read the configuration file in test_config.py


import configparser
 
#Instantiate configParser object
config = configparser.ConfigParser()
#- read read read ini file
config.read('C:\\Users\\songlihui\\PycharmProjects\\AutoTest_02\\config\\config.ini', encoding='GB18030')
#- sections gets all sections and returns them as a list
print('sections:' , ' ' , config.sections())
 
#- options(section) gets all options of the section
print('options:' ,' ' , config.options('config'))
 
#- items (section) gets all key value pairs of the section
print('items:' ,' ' ,config.items('cmd'))
 
#- get(section,option) gets the value of option in section and returns it as string type
print('get:' ,' ' , config.get('cmd', 'startserver'))
 
#- getint(section,option) gets the value of option in section, and returns int
print('getint:' ,' ' ,config.getint('cmd', 'id'))
print('getfloat:' ,' ' , config.getfloat('cmd', 'weight'))
print('getboolean:' ,'  ', config.getboolean('cmd', 'isChoice'))
"""
//First, get all the groups of the configuration file, and then show them one by one
"""
for sections in config.sections():
    for items in config.items(sections):
        print(items)


Execution result:


C:\Users\think\.virtualenvs\RDPGUI-Oi_XG-yi\Scripts\python.exe D:/Raspberry pie development/RDPGUI/test/test_config.py
sections:   ['config', 'cmd', 'log']
options:   ['platformname', 'apppackage', 'appactivity']
items:   [('viewphone', 'adb devices'), ('startserver', 'adb start-server'), ('stopserver', 'adb kill-server'), ('install', 'adb install aaa.apk'), ('id', '1'), ('weight', '12.1'), ('ischoice', 'True')]
get:   adb start-server
getint:   1
getfloat:   12.1
getboolean:    True
('platformname', 'Android')
('apppackage', 'com.romwe')
('appactivity', 'com.romwe.SplashActivity')
('viewphone', 'adb devices')
('startserver', 'adb start-server')
('stopserver', 'adb kill-server')
('install', 'adb install aaa.apk')
('id', '1')
('weight', '12.1')
('ischoice', 'True')
('log_error', 'true')
Process finished with exit code 0



Tags: Python Android encoding

Posted on Fri, 08 Nov 2019 09:41:21 -0500 by .evo.