我有一个配置文件,我使用标准ConfigParser库中的RawConfigParser读取.我的配置文件有一个[DEFAULT]部分,后跟一个[特定]部分.当我遍历[specific]部分中的选项时,它包含[DEFAULT]下的选项,这就是要发生的事情.
但是,对于报告,我想知道该选项是在[specific]部分还是在[DEFAULT]中设置的.有没有办法用RawConfigParser的界面做到这一点,或者我没有选择,只能手动解析文件?(我已经看了一下,我开始担心最糟糕的......)
例如
[默认]
name = a
姓= b
[部分]
name = b
年龄= 23岁
你怎么知道,使用RawConfigParser接口,是否从[DEFAULT]或section [SECTION]部分加载了选项名称和姓氏?
(我知道[DEFAULT]适用于所有人,但你可能想在内部报告这样的事情,以便通过复杂的配置文件工作)
谢谢!
我想使用配置文件和一些简单的数学表达式,如添加或减少.
例如:
[section]
a = 10
b = 15
c = a-5
d = b+c
Run Code Online (Sandbox Code Playgroud)
有没有办法使用ConfigParser模块?我发现了一些在配置文件中使用字符串作为变量的例子,但如果我使用它,我会得到一个未计算的字符串(我必须在我的python代码中解析它).
如果在ConfigParser中不可能,您推荐使用任何模块吗?
我想使用python修改samba配置文件.这是我的代码
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read( '/etc/samba/smb.conf' )
for section in parser.sections():
print section
for name, value in parser.items( section ):
print ' %s = %r' % ( name, value )
Run Code Online (Sandbox Code Playgroud)
但配置文件包含选项卡,是否有可能忽略选项卡?
ConfigParser.ParsingError: File contains parsing errors: /etc/samba/smb.conf
[line 38]: '\tworkgroup = WORKGROUP\n'
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Python中读取INI格式的项目文件。该文件在一个节中包含重复的键(具有唯一值)。例如,以下部分之一如下所示:
[Source Files]
Source="file1.c"
Source="file2.c"
Source="file3.c"
Run Code Online (Sandbox Code Playgroud)
如果我使用以下代码阅读
config = configparser.ConfigParser( strict=False )
config.read( "project/file/name" )
print( config.get( "Source Files", "Source" ) )
Run Code Online (Sandbox Code Playgroud)
结果是
"file3.c"
Run Code Online (Sandbox Code Playgroud)
有什么办法Source代替获取键的所有值的列表吗?我愿意使用其他方法来解析文件。
请注意,我无法更改文件格式。
我正在尝试使用 ConfigParser 为我的 pygame 游戏读取 .cfg 文件。由于某种原因,我无法让它发挥作用。代码如下所示:
import ConfigParser
def main():
config = ConfigParser.ConfigParser()
config.read('options.cfg')
print config.sections()
Screen_width = config.getint('graphics','width')
Screen_height = config.getint('graphics','height')
Run Code Online (Sandbox Code Playgroud)
此文件中的 main 方法在游戏的启动器中调用。我已经测试过了,效果很好。当我运行此代码时,出现此错误:
Traceback (most recent call last):
File "Scripts\Launcher.py", line 71, in <module>
Game.main()
File "C:\Users\astro_000\Desktop\Mini-Golf\Scripts\Game.py", line 8, in main
Screen_width = config.getint('graphics','width')
File "c:\python27\lib\ConfigParser.py", line 359, in getint
return self._get(section, int, option)
File "c:\python27\lib\ConfigParser.py", line 356, in _get
return conv(self.get(section, option))
File "c:\python27\lib\ConfigParser.py", line 607, in get
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'graphics'
Run Code Online (Sandbox Code Playgroud)
问题是,有一个“图形”部分。
我试图读取的文件如下所示: …
我正在尝试在运行 OS X Yosemite (10.10.5) 的 Mac 上使用以下 Python 脚本连接到我的 ftp 服务器:
from ftplib import FTP
import ConfigParser
config_file = "ftp.config"
config = ConfigParser.ConfigParser()
try:
config.read(config_file)
except:
raise Exception("Failed reading {} file.").format(config_file)
server_address = config.get("main", "server")
user_name = config.get("main", "user_name")
password = config.get("main", "password")
ftp = FTP(server_address)
ftp.login(user_name, password)
print "Logging in as '{}'".format(user_name)
Run Code Online (Sandbox Code Playgroud)
该脚本从文件“ftp.config”中获取服务器信息,内容如下:
[main]
enable = 1
server = "my.ftp.address"
user_name = "some_user_name"
password = "some_password"
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时,出现以下错误:
Traceback (most recent call last):
File "ftp2.py", line 34, in <module> …Run Code Online (Sandbox Code Playgroud) 我正在使用Argparse从配置文件中获取必要的值。
例如:
python arg.py --event_conf=/opt/open-stack-tools/track_events.conf --openstack_conf=/etc/nova/nova.conf
Run Code Online (Sandbox Code Playgroud)
我需要从两个不同的文件中获取值。
我可以根据需要获取一个本地配置文件的结果。
但是,如果从nova.conf文件中获取必要的值,则会导致以下错误:
Traceback (most recent call last):
File "arg.py", line 36, in <module>
oslo_messaging_rabbit= dict(config.items("oslo_messaging_rabbit"))
File "/usr/lib/python2.7/ConfigParser.py", line 655, in items
for option in options]
File "/usr/lib/python2.7/ConfigParser.py", line 691, in _interpolate
self._interpolate_some(option, L, rawval, section, vars, 1)
File "/usr/lib/python2.7/ConfigParser.py", line 723, in _interpolate_some
option, section, rest, var)
ConfigParser.InterpolationMissingOptionError: Bad value substitution:
section: [oslo_messaging_rabbit]
option : logging_exception_prefix
key : color
rawval : %(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s
Run Code Online (Sandbox Code Playgroud)
有没有办法解决相同的问题。
我已经复制了必要的内容并创建了一个新的本地文件,我可以看到它运行正常。
当我使用nova.conf文件时,将导致错误。
我无法更改正在使用的文件。
因此,我需要针对特定错误的修复程序。
注意:
根据需要添加更多详细信息:
parser.add_argument("-c", …Run Code Online (Sandbox Code Playgroud) 我不确定我做错了什么.以前的代码是这样的:
volume = min(60, max(30, volume))
Run Code Online (Sandbox Code Playgroud)
但是在尝试使用configparser之后,我的Twilio Server上出现了500错误.
volume = min(configParser.get('config_searchandplay', 'volume_max'), max(configParser.get('config_searchandplay', 'volume_min'), volume)) #Max Volume Spam Protection
Run Code Online (Sandbox Code Playgroud)
CONFIG.INI
[config_searchandplay]
#Volume Protection
volume_max = 90
volume_min = 10
Run Code Online (Sandbox Code Playgroud) 在我的配置文件中,我有类似的内容:
[Section_1]
List=Column1,Column2,Column3,Column4
Run Code Online (Sandbox Code Playgroud)
现在,我想在我的主文件中将它作为普通列表处理:
config = configparser.ConfigParser()
config.read("configTab.ini")
for index in range(len(List)):
sql=sql.replace(List[index],"replace("+List[index]+","'hidden'")")
Run Code Online (Sandbox Code Playgroud)
现在,当我从配置文件中读取时,“列表”是一个普通的字符串。最好的方法是什么?
如果我以这种方式在主代码中放置一个普通列表变量:
List=['Column1','Column2','Column3','Column4']
Run Code Online (Sandbox Code Playgroud)
然后它工作正常,但我想从我的配置文件中获取它,
谢谢
我正在使用 configparser 来自动读取和修改名为“streamer.conf”的文件 conf。我正在这样做:
import configparser
config = configparser.ConfigParser()
config.read('C:/Users/../Desktop/streamer.conf')
Run Code Online (Sandbox Code Playgroud)
然后它与此错误消息分开:
MissingSectionHeaderError: File contains no section headers.
file: 'C:/Users/../Desktop/streamer.conf', line: 1
u'input{\n'
Run Code Online (Sandbox Code Playgroud)
可能有什么问题?任何帮助表示赞赏。
python configuration configuration-files configparser python-2.7
configparser ×10
python ×9
python-2.7 ×2
config ×1
ftp ×1
ftplib ×1
parsing ×1
pygame ×1
python-3.x ×1