日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 读取配置文件config_python中读取配置文件ConfigParser

發布時間:2024/10/14 python 70 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 读取配置文件config_python中读取配置文件ConfigParser 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在程序中使用配置文件來靈活的配置一些參數是一件很常見的事情,配置文件的解析并不復雜,在python里更是如此,在官方發布的庫中就包含有做這件事情的庫,那就是ConfigParser,這里簡單的做一些介紹。

ConfigParser解析的配置文件的格式比較象ini的配置文件格式,就是文件中由多個section構成,每個section下又有多個配置項,比如:

[db]

db_host=127.0.0.1

db_port=3306

db_user=root

db_pass=password

[concurrent]

thread=10

processor=20

_______________________________________________________________________________________________________________________________

#!/usr/bin/python

#coding:utf-8

import ConfigParser

import string,os,sys

cf = ConfigParser.ConfigParser()

#讀取配置文件

cf.read("test.conf")

#返回section,即[]中的內容

s = cf.sections()

print 'section:', s

#返回db section中的選項

o = cf.options("db")

print 'options:',o

#以列表形式返回db section中選項和值

v = cf.items("db")

print 'db:',v

print '-'*60

db_host = cf.get("db","db_host")

db_port = cf.getint("db","db_port")

db_user = cf.get("db","db_user")

db_pass = cf.get("db","db_pass")

#返回整型

threads = cf.getint("concurrent","thread")

processors = cf.getint("concurrent","processor")

print "db_host:", db_host

print "db_port:", db_port

print "db_user:", db_user

print "db_pass:", db_pass

print "thread:", threads

print "processor:", processors

#修改一個值,再寫回去

cf.set("db","db_pass","test")

cf.write(open("test.conf","w"))

總結

以上是生活随笔為你收集整理的python 读取配置文件config_python中读取配置文件ConfigParser的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。