哪个更适合为Python程序,内置模块(ConfigParser)或独立项目(ConfigObj)创建设置文件?
我的ConfigParser麻烦还在继续.它似乎不能很好地支持Unicode.配置文件确实保存为UTF-8,但是当ConfigParser读取它时,它似乎被编码成其他东西.我以为它是latin-1而我知道压倒optionxform可能会有所帮助:
-- configfile.cfg --
[rules]
Häjsan = 3
? = my snowman
-- myapp.py --
# -*- coding: utf-8 -*-
import ConfigParser
def _optionxform(s):
try:
newstr = s.decode('latin-1')
newstr = newstr.encode('utf-8')
return newstr
except Exception, e:
print e
cfg = ConfigParser.ConfigParser()
cfg.optionxform = _optionxform
cfg.read("myconfig")
Run Code Online (Sandbox Code Playgroud)
当然,当我读到配置时,我得到:
'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几种不同的解码方法,但这一点似乎没有实际意义,因为它从一开始就应该是一个unicode对象.毕竟,配置文件是UTF-8?我已经确认ConfigParser通过使用此DummyConfig类将其删除来读取文件的方式有问题.如果我使用它,那么一切都很好unicode,罚款和花花公子.
-- config.py --
# -*- coding: utf-8 -*-
apa = {'rules': [(u'Häjsan', 3), (u'?', u'my snowman')]}
class DummyConfig(object): …Run Code Online (Sandbox Code Playgroud) 我最近介绍了库configparser.我希望能够检查每个部分是否至少有一个布尔值设置为1.例如:
[Horizontal_Random_Readout_Size]
Small_Readout = 0
Medium_Readout = 0
Large_Readout = 0
Run Code Online (Sandbox Code Playgroud)
以上会导致错误.
[Vertical_Random_Readout_Size]
Small_Readout = 0
Medium_Readout = 0
Large_Readout = 1
Run Code Online (Sandbox Code Playgroud)
以上将通过.下面是我想到的一些伪代码:
exit_test = False
for sections in config_file:
section_check = False
for name in parser.options(section):
if parser.getboolean(section, name):
section_check = True
if not section_check:
print "ERROR:Please specify a setting in {} section of the config file".format(section)
exit_test = True
if exit_test:
exit(1)
Run Code Online (Sandbox Code Playgroud)
问题:
1)如何执行第一个for循环并迭代配置文件的各个部分?
2)这是一个很好的方法吗?还是有更好的方法?(如果没有请回答问题一.)
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('test.ini')
Run Code Online (Sandbox Code Playgroud)
这就是我们在Python中读取配置文件的方式.但如果'test.ini'不存在怎么办?为什么这个方法不会抛出异常?
如果文件不存在,如何使它抛出异常?
是否可以ConfigParser从字符串或列表中读取配置?
文件系统上没有任何临时文件
或者
有没有类似的解决方案?
我有一个配置文件如下:
[job]
mailto=bob
logFile=blahDeBlah.txt
Run Code Online (Sandbox Code Playgroud)
我想阅读使用的选项SafeConfigParser:
values = {}
config = ConfigParser.SafeConfigParser()
try:
config.read(configFile)
jobSection = 'job'
values['mailto'] = config.get( jobSection, 'mailto' )
values['logFile'] = config.get( jobSection, 'logFile' )
# it is not there
values['nothingThere'] = config.get( jobSection, 'nothingThere' )
.... # rest of code
Run Code Online (Sandbox Code Playgroud)
最后一行当然会抛出一个错误.如何为config.get()方法指定默认值?
然后,如果我有一个选项文件如下:
[job1]
mailto=bob
logFile=blahDeBlah.txt
[job2]
mailto=bob
logFile=blahDeBlah.txt
Run Code Online (Sandbox Code Playgroud)
似乎没有办法为job1部分中的默认选项指定默认选项job2.
考虑以下INI文件:
[TestSettings]
# First comment goes here
environment = test
[Browser]
# Second comment goes here
browser = chrome
chromedriver = default
...
Run Code Online (Sandbox Code Playgroud)
我正在使用Python 2.7来更新ini文件:
config = ConfigParser.ConfigParser()
config.read(path_to_ini)
config.set('TestSettings','environment',r'some_other_value')
with open(path_to_ini, 'wb') as configfile:
config.write(configfile)
Run Code Online (Sandbox Code Playgroud)
如何在不删除注释的情况下更新INI文件.INI文件已更新,但注释已删除.
[TestSettings]
environment = some_other_value
[Browser]
browser = chrome
chromedriver = default
Run Code Online (Sandbox Code Playgroud) 使用Python ConfigParser,是否可以跨外部使用插值?我的想法似乎告诉我,我已经看到它可能在某个地方,但我在搜索时找不到它.
这个例子不起作用,但它是为了让我知道我正在尝试做什么.
[section1]
root = /usr
[section2]
root = /usr/local
[section3]
dir1 = $(section1:root)/bin
dir2 = $(section2:root)/bin
Run Code Online (Sandbox Code Playgroud)
请注意,我使用的是Python 2.4.
所以我正在编写一个从配置文件中读取的脚本,我想准确地使用它如何设计如下所述:http://docs.python.org/release/3.2.1/library/ configparser.html
我使用的是Python 3.2.1.完成后,该脚本将在使用相同版本的Python的Windows 2008 R2计算机上运行,或者假定兼容性,即当时的最新版本.
#!/user/bin/env python
import configparser
config = configparser.ConfigParser()
config.read('c:\exclude.ini')
config.sections()
Run Code Online (Sandbox Code Playgroud)
这可以正常读取exclude.ini文件 - 除非我有一个没有键的值.想到我可能做错了什么尝试解析这里列出的例子:http://docs.python.org/release/3.2.1/library/configparser.html#supported-ini-file-structure
它每次都会引发以下情况:
File "C:\Python32\lib\configparser.py", line 1081, in _read
raise e
configparser.ParsingError: Source contains parsing errors: c:\exclude.ini
[line 20]: 'key_without_value\n'
Run Code Online (Sandbox Code Playgroud)
我很茫然...我真的从文档中复制/粘贴我正在使用的精确python版本的示例代码,但它不能正常工作.我只能假设我错过了一些东西,因为我也找不到任何有类似问题的人.
这有点我没有python语法,我在读取.ini带插值的文件时遇到问题.
这是我的ini文件:
[DEFAULT]
home=$HOME
test_home=$home
[test]
test_1=$test_home/foo.csv
test_2=$test_home/bar.csv
Run Code Online (Sandbox Code Playgroud)
那些台词
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('config.ini')
print parser.get('test', 'test_1')
Run Code Online (Sandbox Code Playgroud)
输出
$test_home/foo.csv
Run Code Online (Sandbox Code Playgroud)
虽然我在期待
/Users/nkint/foo.csv
Run Code Online (Sandbox Code Playgroud)
我认为$语法隐含在所谓的字符串插值中(参考手册):
除了核心功能之外,SafeConfigParser还支持插值.这意味着值可以包含引用同一节中其他值的格式字符串,或特殊DEFAULT节中的值.
但我错了.如何处理这种情况?
configparser ×10
python ×10
python-2.7 ×2
configobj ×1
default ×1
exception ×1
file ×1
ini ×1
python-2.4 ×1
python-2.x ×1
python-3.x ×1
sections ×1
unicode ×1