标签: configparser

Python 3 ConfigParser 也读取内联注释

我有以下代码,其中filePath是磁盘上 cfg 文件的路径。当我解析它时,它还会读取内联注释(带有空格 + 的注释";")。

结果的一些行:

xlsx:是的;评论在这里

html:是的;评论在这里

它应该是:

xlsx:是的

html:是的

def ParseFile(filePath):
    """this function returns the parsed CFG file"""
    parser = configparser.ConfigParser()
    print("Reading config file from %s" % filePath)
    parser.read(filePath)
    for section in parser.sections():
        print("[ SECTION: %s ]" % section)
        for option in parser.options(section):
            print("%s:%s" % (option, parser.get(section, option)))
Run Code Online (Sandbox Code Playgroud)

parsing config configparser python-3.x

3
推荐指数
1
解决办法
1097
查看次数

不明白这个 ConfigParser.InterpolationSyntaxError

所以我试图为我的脚本编写一个小的配置文件,它应该指定一个 IP 地址、一个端口和一个 URL,这些文件应该通过使用前者到变量的插值来创建。我的config.ini看起来像这样:

[Client]
recv_url : http://%(recv_host):%(recv_port)/rpm_list/api/
recv_host = 172.28.128.5
recv_port = 5000
column_list = Name,Version,Build_Date,Host,Release,Architecture,Install_Date,Group,Size,License,Signature,Source_RPM,Build_Host,Relocations,Packager,Vendor,URL,Summary
Run Code Online (Sandbox Code Playgroud)

在我的脚本中,我解析这个配置文件如下:

config = SafeConfigParser()
config.read('config.ini')
column_list = config.get('Client', 'column_list').split(',')
URL = config.get('Client', 'recv_url')
Run Code Online (Sandbox Code Playgroud)

如果我运行我的脚本,这会导致:

config = SafeConfigParser()
config.read('config.ini')
column_list = config.get('Client', 'column_list').split(',')
URL = config.get('Client', 'recv_url')
Run Code Online (Sandbox Code Playgroud)

我试过调试,结果又给了我一行错误代码:

Traceback (most recent call last):
  File "server_side_agent.py", line 56, in <module>
    URL = config.get('Client', 'recv_url')
  File "/usr/lib64/python2.7/ConfigParser.py", line 623, in get
    return self._interpolate(section, option, value, d)
  File "/usr/lib64/python2.7/ConfigParser.py", line 691, in _interpolate
    self._interpolate_some(option, L, …
Run Code Online (Sandbox Code Playgroud)

python configparser string-interpolation

3
推荐指数
1
解决办法
2734
查看次数

在python中解析.ini文件

根据文档(https://docs.python.org/3/library/configparser.html),我使用configparser来解析 .ini 文件

代码 :

import ConfigParser
config = ConfigParser.ConfigParser(allow_no_value=True)
config.read('D:\\test\\sample.ini')
print(config.sections())
Run Code Online (Sandbox Code Playgroud)

示例 ini 文件 1:(工作)

[Group1]   
test_value1=0  
test_value2=5
Run Code Online (Sandbox Code Playgroud)

此代码正在运行,并且成功加载示例 ini file1

但很少有以下 ini 文件没有使用上面的代码进行解析,有人可以帮助我吗?

示例 ini 文件 2:(不工作)

[Group1]   
    test_value1=0  
    test_value2=5
Run Code Online (Sandbox Code Playgroud)

示例 ini 文件 3:(不工作)

   [Group1]
     [[inner_group1]]
       test_value1=0 
       test_value2=5
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏。

谢谢,
哈利

python configparser python-2.7 python-3.x

3
推荐指数
1
解决办法
3173
查看次数

如何使用配置解析器解析布尔值

使用 python 的内置函数configparser,我想解析我的conf文件中的布尔值。

例子:

[SECTION]
foo = False
Run Code Online (Sandbox Code Playgroud)

但是,当访问变量时,我注意到它被作为字符串处理。

>>> config['SECTION']['foo']
'False'
Run Code Online (Sandbox Code Playgroud)

此外,当我尝试纠正此行为并将密钥重新分配foo给它正确的布尔代表时,我收到此错误

>>>         if config['SECTION']['foo'] == 'True':
...             config['SECTION']['foo'] = True
...         elif config['SECTION']['foo'] == 'False':                                                                                                                                      
...             config['SECTION']['foo'] = False
...         else:                                    
...             Exception("foo must be bool")
TypeError: option values must be strings                                                                                                                                                      
Run Code Online (Sandbox Code Playgroud)

不幸的是,这种行为会导致出现以下问题:

print(config['SECTION']['foo']) # 'False'

if config['SECTION']['foo']:
   print('do things when foo is True') # this runs, but foo actually 
                                       # represents false, but in string form
Run Code Online (Sandbox Code Playgroud)

在解析时我应该如何以configparser尽可能少的开销处理布尔值?

python ini configparser

3
推荐指数
1
解决办法
4059
查看次数

Python configparser从S3读取配置而不下载

有没有办法从 s3 读取 .ini 配置文件而不下载它?

我尝试过的:

配置.ini:

[DEFAULT]
test = test1
test1 = test2

[ME]
me = you
you = he
Run Code Online (Sandbox Code Playgroud)

代码:

import boto3
import io
import configparser

s3_boto = boto3.client('s3')
configuration_file_bucket = "mybucket"
configuration_file_key = "config.ini"
obj = s3_boto.get_object(Bucket=configuration_file_bucket, Key=configuration_file_key)

config = configparser.ConfigParser()
config.read(io.BytesIO(obj['Body'].read()))
Run Code Online (Sandbox Code Playgroud)

它返回[]。

我试图确保

obj['Body'].read()
Run Code Online (Sandbox Code Playgroud)

很好地返回了包含 config.ini 内容的二进制文件。这是有效的。它在更远的地方破裂了。

python amazon-s3 configparser

3
推荐指数
1
解决办法
4591
查看次数

从 python 中的 configparser 读取时,如何将缩进包含在多行值中?

我有一些像这样的配置文件:

[main]
key_one   =
  hello
    this
      is
        indented
Run Code Online (Sandbox Code Playgroud)

但是当我使用 python 中的 configparser 库读取它时,如下所示:

[main]
key_one   =
  hello
    this
      is
        indented
Run Code Online (Sandbox Code Playgroud)

打印出:

hello
this
is
indented
Run Code Online (Sandbox Code Playgroud)

我尝试过使用制表符,每个缩进超过两个空格,但似乎没有任何效果。如何让 configparser 识别缩进?

python ini configparser

3
推荐指数
1
解决办法
1412
查看次数

如何使用 python configparser 读取缩进部分

我尝试使用 python configparser 读取以下配置文件:

# test.conf
[section]
a = 0.3

        [subsection]
        b = 123
Run Code Online (Sandbox Code Playgroud)
# test.conf
[section]
a = 0.3

        [subsection]
        b = 123
Run Code Online (Sandbox Code Playgroud)

输出:

0.3

[subsection]
b = 123
Run Code Online (Sandbox Code Playgroud)

如果我删除缩进,则 a 会被正确读取。

如何使用 python configparser 正确读取带有缩进的配置文件?

根据文档,它应该可以工作:
https ://docs.python.org/3.8/library/configparser.html#supported-ini-file-struct

我使用Python 3.7.6

python configparser

3
推荐指数
1
解决办法
2036
查看次数

Python错误:ConfigParser.NoSectionError:没有部分:

事先道歉,因为我确信这是一个非常基本的错误,但我找不到答案.我对Python很新,并希望使用ConfigParser模块读取配置文件.这是我的配置文件:

[Server]
server_name= localhost
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

#!/usr/bin/env python

import ConfigParser

config = ConfigParser.ConfigParser()
config.read=('config.cfg')

print config.get('Server', 'server_name')
Run Code Online (Sandbox Code Playgroud)

我尝试运行它,它给了我错误:

    File "./read_config.py", line 10, in <module>
    print config.get('Server', 'server_name')
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ConfigParser.py", line 531, in get
    raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'Server'
Run Code Online (Sandbox Code Playgroud)

它找不到服务器部分.config.cfg文件与Python脚本位于同一目录中,我甚至提供了完整路径,但仍然出现相同的错误.我已经在OSX和Debian 6上尝试过Python 2.6和2.7.我很尴尬地说我很难过.我在这里失踪了什么?

python configparser

2
推荐指数
1
解决办法
2万
查看次数

ConfigParser从INI文件中获取值,其中包含section和subsection,如下所示

我有以下类型的INI文件

[section1][subsection1]
port=989
[section1][subsection2]
somethign=somethign 
Run Code Online (Sandbox Code Playgroud)

我正在使用Python的ConfigParser来解析INI文件,但我无法弄清楚如何从上面的INI文件中获取数据.

以下代码用于获取INI文件时的值

[section1]
port=908
[section2]
ss=ss

config = ConfigParser.RawConfigParser()
config.read(INI_File)
mIp =  config.get('section1','port')
Run Code Online (Sandbox Code Playgroud)

请不要建议我更改INI文件格式:)

谢谢

python ini configparser

2
推荐指数
1
解决办法
9824
查看次数

Yosemite安装后的Python configparser错误

我升级到优胜美地,这似乎打破了我的python模块.

python --version == Python 2.7.6

然后从Python shell:

>>> import pyrax
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/pyrax/__init__.py", line 38, in <module>
    import six.moves.configparser as ConfigParser
ImportError: No module named configparser
Run Code Online (Sandbox Code Playgroud)

所以它抱怨configparser.

pip show configparser
---
Name: configparser
Version: 3.3.0r2
Location: /Library/Python/2.7/site-packages
Requires: 
Run Code Online (Sandbox Code Playgroud)

但它就在那里.经过一些阅读后,很明显ConfigParser已经在python版本3中重命名为configparser.但是我运行2.7.

---编辑----

更多信息:
没有在Python上使用自制软件
我试过重新安装pyrax

有任何想法吗 ?

python configparser osx-yosemite

2
推荐指数
2
解决办法
3368
查看次数