python 读配置文件

python读conf配置文件--ConfigParser

python读写配置文件还是比较方便得。

配置文件的格式是: []包含的叫section,    section 下有option=value这样的键值

配置文件   test.conf

[section1]
name = tank
age = 28

[section2]
ip = 192.168.1.1
port = 8080

1) 基本的读取配置文件
     -read(filename) 直接读取ini文件内容
     -sections() 得到所有的section,并以列表的形式返回
     -options(section) 得到该section的所有option
     -items(section) 得到该section的所有键值对
     -get(section,option) 得到section中option的值,返回为string类型
     -getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
 
2) 基本的写入配置文件
     -add_section(section) 添加一个新的section
     -set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。

时间: 2024-08-05 10:59:03

python 读配置文件的相关文章

python读取配置文件的方式

python读取配置文件的方式 1.从config.ini中读取,后缀无所谓,文件名字也无所谓,不过config.ini是常用写法,所谓见名知意 config.ini内容: [global] ip = xxx port = xxx table = xxx uname = xxx passwd = xxx 读取方法 import configparser import os dir_now = os.path.dirname(os.path.dirname(os.path.abspath("set

静态读配置文件

/** * 读配置文件 * @param configPath 配置文件路径 * @param key * @return */ public static String getPropertyFromConfigByKey(String configPath, String key){ Properties pros=new Properties(); InputStream is=Utils.class.getResourceAsStream(configPath); try { pros.

java工具类-读配置文件

///读配置文件 import java.io.InputStream;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;import java.util.Properties;import java.util.Set;public class PropertiesUtils{ private static Map<String, String> propertiesMap = new HashMa

python 读 excel 模块: xlrd

主要来自:[ python中使用xlrd.xlwt操作excel表格详解 ] 为了方便阅读, 我将原文两个模块拆分为两篇博文: [ python 读 excel 模块: xlrd ] [ python 写 excel 模块: xlwt ] xlrd 基本操作 这个过程有几个比较麻烦的问题,比如读取日期.读合并单元格内容. 读一个有2个 sheet 的 excel 文件, 读入sheet2的内容, 内容如下: 使用 python 读入 #!/usr/bin/env python # -*- cod

python读取配置文件 变量 ConfigParser模块

Python 读取写入配置文件很方便,可使用内置的 configparser 模块配置文件:config.ini [oppo] platformName = Android platformVersion = 6.0 deviceName = weiruoyu appPackage = com.sina.weibo appActivity = .SplashActivity url = http://127.0.0.1:4723/wd/hub [mysql] host=127.0.0.1 por

Python读文件的几种方式

介绍 本文介绍在python中读取文件的方法. 正文 1. f = open(input_file) for line in f:     do_stuff(line)f.close() 2. for line in open('myfile','r').readlines():     do_something(line) readlines()读文件的时候会把整个文件一次读到内存,所以这种方法不适合读取大文件. 3. import fileinput for line in fileinpu

Python中配置文件编写configparser

用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下 1 2 3 4 5 6 7 8 9 10 11 12 [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 500

python 读取配置文件的值

上例子: 假设配置文件如下,文件名为test.cfg #------------------------------------ #abcdefg abc = 1 #----------------------------------------- 其中的#号后面的部分都是注释 读入该文件的python代码为: from types import ModuleType import re cfg = [] fcfg = "D:/test.cfg" content = {} try: e

python读取配置文件 ConfigParser

Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 option: b) section 用 [sect_name] 表示,每个option是一个键值对,使用分隔符 = 或 : 隔开: c) 在 option 分隔符两端的空格会被忽略掉 d) 配置文件使用 # 和 ; 注释 一个简单的配置文件样例 myapp.conf 1 2 3 4 5 6 7 8 9