1.Python ConfigParser 可读取哪些类型的数据
测试配置文件test.conf内容如下:
复制代码代码如下:
[first]
w = 2
v: 3
c =11-3
[second]
sw=4
test: hello
测试配置文件中有两个区域,first和second,另外故意添加一些空格、换行。
下面解析:
复制代码代码如下:
>>> import ConfigParser
>>> conf=ConfigParser.ConfigParser()
2.python configparser section 是个什么概念
1、获取所有节点
import configparser
config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8')
ret = config.sections()
print(ret)
2、获取指定节点下所有的键值对
import configparser
config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8')
ret = config.items('section1')
print(ret)
3.python configparser怎么写default
以这个非常简单的典型配置文件为例:
[DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLevel = 9ForwardX11 = yes[bitbucket.org]User = hg[topsecret.server.com]Port = 50022ForwardX11 = no123456789101112
1、config parser 操作跟dict 类似,在数据存取方法基本一致
>> import configparser>>> config = configparser.ConfigParser()>>> config.sections()
[]>>> config.read('example.ini')
['example.ini']>>> config.sections()
['bitbucket.org', 'topsecret.server.com']>>> 'bitbucket.org' in configTrue>>> 'bytebong.com' in configFalse>>> config['bitbucket.org']['User']'hg'>>> config['DEFAULT']['Compression']'yes'>>> topsecret = config['topsecret.server.com']>>> topsecret['ForwardX11']'no'>>> topsecret['Port']'50022'>>> for key in config['bitbucket.org']: print(key)
user
compressionlevel
serveraliveinterval
compression
forwardx11>>> config['bitbucket.org']['ForwardX11']'yes'
4.python 运行报错 no module named configparser
>> import configparser
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named configparser因为你没有这个类,python有个类名字叫ConfigParser ,是用来做配置解析的
不过看你这情况有可能是拼写错误,应当为大写的ConfigParser
转载请注明出处代码入门网 » pythonconfigparser