我有一个我需要使用Python修改的INI文件.我正在调查该ConfigParser模块,但仍然遇到麻烦.我的代码是这样的:
config= ConfigParser.RawConfigParser()
config.read('C:\itb\itb\Webcams\AMCap1\amcap.ini')
config.set('Video','Path','C:\itb\itb')
Run Code Online (Sandbox Code Playgroud)
但是amcap.ini在运行此代码后查看文件时,它仍未修改.谁能告诉我我做错了什么?
这有点我没有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模块时,我想使用包含cfg文件中设置的多个单词的值.在这种情况下,使用像(example.cfg)这样的引号来包围字符串似乎微不足道:
[GENERAL]
onekey = "value in some words"
Run Code Online (Sandbox Code Playgroud)
我的问题是,在这种情况下,python在使用这样的值时也会将引号附加到字符串:
config = ConfigParser()
config.read(["example.cfg"])
print config.get('GENERAL', 'onekey')
Run Code Online (Sandbox Code Playgroud)
我确信有一个内置功能来管理只打印'value in some words'而不是'"value in some words"'.这怎么可能?谢谢.
我在Python中使用ConfigParser
config.ini是
[general]
name: my_name
base_dir: /home/myhome/exp
exe_dir: ${base_dir}/bin
Run Code Online (Sandbox Code Playgroud)
在这里我想exp_dir变得/home/myhome/exp/bin不${base_dir}/bin.
它意味着${base_dir}将被替代/home/myhome/exp automatically.
这是我的示例脚本:
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('conf.ini')
print bool(config.get('main', 'some_boolean'))
print bool(config.get('main', 'some_other_boolean'))
Run Code Online (Sandbox Code Playgroud)
这是conf.ini:
[main]
some_boolean: yes
some_other_boolean: no
Run Code Online (Sandbox Code Playgroud)
运行脚本时,它会打印True两次.为什么?它应该是False,如some_other_boolean设置的那样no.
使用ConfigParser的has_section()方法,我可以检查文件中是否存在某个部分,例如:
config.has_section(section_name)
Run Code Online (Sandbox Code Playgroud)
什么是检查密钥是否存在的命令?因此,在使用以下方法查询值之前,可以验证段和键是否存在:
value = config.get(section, key)
Run Code Online (Sandbox Code Playgroud)
提前致谢!
ConfigParser和之间有什么区别SafeConfigParser?那么,为什么后者更安全呢?什么是'不安全' ConfigParser?我知道SafeConfigParser继承了ConfigParser它,它做了什么不同?
我有以下内容:
config = ConfigParser()
config.read('connections.cfg')
sections = config.sections()
Run Code Online (Sandbox Code Playgroud)
如何关闭打开的文件config.read?
就我而言,随着新的部分/数据被添加到config.cfg文件中,我更新了我的wxtree小部件.但是,它只更新一次,我怀疑是因为config.read保持文件打开.
虽然我们在这里,但ConfigParser和之间的主要区别是RawConfigParser什么?
如何使用python configparser模块解析ini文件中没有值的标签?
例如,我有以下ini,我需要解析rb.在一些ini文件中,rb具有整数值,而在某些文件中没有任何值,如下例所示.如何在没有获得valueerror的情况下使用configparser执行此操作?我使用getint函数
[section]
person=name
id=000
rb=
Run Code Online (Sandbox Code Playgroud) 我需要能够使用它ConfigParser来读取同一个键的多个值.示例配置文件:
[test]
foo = value1
foo = value2
xxx = yyy
Run Code Online (Sandbox Code Playgroud)
使用"标准"使用时ConfigParser,将有一个foo具有该值的键value2.但我需要解析器读入两个值.
在重复键上输入后,我创建了以下示例代码:
from collections import OrderedDict
from ConfigParser import RawConfigParser
class OrderedMultisetDict(OrderedDict):
def __setitem__(self, key, value):
try:
item = self.__getitem__(key)
except KeyError:
super(OrderedMultisetDict, self).__setitem__(key, value)
return
print "item: ", item, value
if isinstance(value, list):
item.extend(value)
else:
item.append(value)
super(OrderedMultisetDict, self).__setitem__(key, item)
config = RawConfigParser(dict_type = OrderedDict)
config.read(["test.cfg"])
print config.get("test", "foo")
print config.get("test", "xxx")
config2 = RawConfigParser(dict_type = OrderedMultisetDict)
config2.read(["test.cfg"])
print …Run Code Online (Sandbox Code Playgroud)