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

ตัวแยกวิเคราะห์ไฟล์การกำหนดค่าใน Python (configparser)


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

ไฟล์ INI ประกอบด้วยส่วนต่างๆ แต่ละส่วนนำโดยส่วนหัว [ส่วน] ระหว่างวงเล็บเหลี่ยม เราสามารถใส่ชื่อส่วนได้ ส่วนตามด้วยรายการคีย์/ค่าที่คั่นด้วยอักขระ =หรือ :อาจรวมถึงความคิดเห็น นำหน้าด้วย # หรือ; สัญลักษณ์. ไฟล์ 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 มีหน้าที่แยกวิเคราะห์รายการไฟล์การกำหนดค่า และจัดการฐานข้อมูลที่แยกวิเคราะห์

Object of ConfigParser ถูกสร้างขึ้นโดยคำสั่งต่อไปนี้ -

parser = configparser.ConfigParser()

วิธีการดังต่อไปนี้ถูกกำหนดไว้ในคลาสนี้ -

sections() ส่งคืนชื่อส่วนการกำหนดค่าทั้งหมด
has_section() ส่งคืนว่าส่วนที่กำหนดมีอยู่หรือไม่
has_option() ส่งคืนว่าตัวเลือกที่กำหนดมีอยู่ในส่วนที่กำหนดหรือไม่
ตัวเลือก() ส่งคืนรายการตัวเลือกการกำหนดค่าสำหรับส่วนที่มีชื่อ
read() อ่านและแยกวิเคราะห์ไฟล์การกำหนดค่าที่มีชื่อ
read_file() อ่านและแยกวิเคราะห์ไฟล์การกำหนดค่า 1 ไฟล์ โดยกำหนดให้เป็นวัตถุไฟล์
read_string() อ่านการกำหนดค่าจากสตริงที่กำหนด
read_dict() อ่านการกำหนดค่าจากพจนานุกรม คีย์คือชื่อส่วน ค่าคือพจนานุกรมที่มีคีย์และค่าที่ควรมีอยู่ในส่วน
get() คืนค่าสตริงสำหรับตัวเลือกที่มีชื่อ
getint() ชอบ get() แต่แปลงค่าเป็นจำนวนเต็ม
getfloat() ชอบ get() แต่แปลงค่าเป็นทศนิยม
getboolean() ชอบ get() แต่แปลงค่าเป็นบูลีน ส่งคืนค่าเท็จหรือจริง
รายการ() แสดงรายการทูเพิลที่มี (ชื่อ ค่า) สำหรับแต่ละตัวเลือกในส่วน
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()