Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ตัวแปลงไฟล์การตั้งค่าใน Python (configparser)

โมดูล configparser จากไลบรารีมาตรฐานของ Python ให้ฟังก์ชันสำหรับอ่านและเขียนไฟล์การตั้งค่า ซึ่งมักใช้ในระบบปฏิบัติการ Microsoft Windows ไฟล์เหล่านี้มักมีนามสกุล .INI

ไฟล์ INI ประกอบด้วยส่วน (sections) แต่ละส่วนนำหน้าด้วยส่วนหัว [section] ภายในวงเล็บเหลี่ยมสามารถใส่ชื่อส่วนได้ ส่วนจะตามด้วยรายการคีย์/ค่าที่คั่นด้วย = หรือ : อาจรวมถึงความคิดเห็นที่นำหน้าด้วย # หรือ ; ตัวอย่างไฟล์ INI ดังนี้:

[Settings]
# Set detailed log for additional debugging info
DetailedLog=1
RunStatus=1
StatusPort=6090
StatusRefresh=10
Archive=1
# Sets the location of the MV_FTP log file
LogFile=/opt/ecs/mvuser/MV_IPTel/log/MV_IPTel.log
Version=0.9 Build 4
ServerName=Unknown

[FTP]
# set the FTP server active
RunFTP=1
# defines the FTP control port
FTPPort=21
# Sets the location of the FTP data directory
FTPDir=/opt/ecs/mvuser/MV_IPTel/data/FTPdata
# set the admin Name
UserName=admin
# set the Password
Password=admin

โมดูล configparser มีคลาส ConfigParser ซึ่งรับผิดชอบในการแปลงไฟล์การตั้งค่าและจัดการฐานข้อมูลที่แปลงแล้ว

การสร้างออบเจกต์ ConfigParser:

parser = configparser.ConfigParser()

เมธอดต่างๆ ที่กำหนดในคลาสนี้:

sections()คืนค่าชื่อส่วนการตั้งค่าทั้งหมด
has_section()ตรวจสอบว่าส่วนที่กำหนดมีอยู่หรือไม่
has_option()ตรวจสอบว่าตัวเลือกที่กำหนดมีอยู่ในส่วนที่กำหนดหรือไม่
options()คืนค่ารายการตัวเลือกการตั้งค่าสำหรับส่วนที่ระบุ
read()อ่านและแปลงไฟล์การตั้งค่าที่ระบุ
read_file()อ่านและแปลงไฟล์การตั้งค่าหนึ่งไฟล์ ที่ให้เป็นออบเจกต์ไฟล์
read_string()อ่านการตั้งค่าจากสตริงที่กำหนด
read_dict()อ่านการตั้งค่าจากดิกชันนารี คีย์คือชื่อส่วน ค่าคือดิกชันนารีที่มีคีย์และค่าที่ควรอยู่ในส่วน
get()คืนค่าสตริงสำหรับตัวเลือกที่ระบุ
getint()เหมือน get() แต่แปลงค่าเป็นจำนวนเต็ม
getfloat()เหมือน get() แต่แปลงค่าเป็นจำนวนจริง
getboolean()เหมือน get() แต่แปลงค่าเป็นบูลีน คืนค่า False หรือ True
items()คืนค่ารายการทูเพิล (name, value) สำหรับแต่ละตัวเลือกในส่วน
remove_section()ลบส่วนไฟล์ที่กำหนดและตัวเลือกทั้งหมด
remove_option()ลบตัวเลือกที่กำหนดจากส่วนที่กำหนด
set()ตั้งค่าตัวเลือกที่กำหนด
write()เขียนสถานะการตั้งค่าในรูปแบบ .ini

สคริปต์ต่อไปนี้อ่านและแปลงไฟล์ 'sampleconfig.ini':

import configparser

parser = configparser.ConfigParser()
parser.read('sampleconfig.ini')

for sect in parser.sections():
    print('Section:', sect)
    for k, v in parser.items(sect):
        print(' {} = {}'.format(k, v))
    print()

ผลลัพธ์

Section: Settings
detailedlog = 1
runstatus = 1
statusport = 6090
statusrefresh = 10
archive = 1
logfile = /opt/ecs/mvuser/MV_IPTel/log/MV_IPTel.log
version = 0.9 Build 4
servername = Unknown

Section: FTP
runftp = 1
ftpport = 21
ftpdir = /opt/ecs/mvuser/MV_IPTel/data/FTPdata
username = admin
password = admin

เมธอด write() ใช้สร้างไฟล์การตั้งค่า สคริปต์ต่อไปนี้กำหนดค่าออบเจกต์ parser และเขียนลงในออบเจกต์ไฟล์ที่แทน 'test.ini':

import configparser

parser = configparser.ConfigParser()
parser.add_section('Manager')
parser.set('Manager', 'Name', 'Ashok Kulkarni')
parser.set('Manager', 'email', 'ashok@gmail.com')
parser.set('Manager', 'password', 'secret')

fp = open('test.ini', 'w')
parser.write(fp)
fp.close()