基于ConfigParser模块,我如何过滤掉并从ini文件中抛出每条注释?
import ConfigParser
config = ConfigParser.ConfigParser()
config.read("sample.cfg")
for section in config.sections():
print section
for option in config.options(section):
print option, "=", config.get(section, option)
Run Code Online (Sandbox Code Playgroud)
例如.在上面基本脚本下面的ini文件中打印出更多注释行,如:
something = 128 ; comment line1
; further comments
; one more line comment
Run Code Online (Sandbox Code Playgroud)
我需要的是只有段名和纯键值对,没有任何注释.ConfigParser是以某种方式处理此问题还是应该使用regexp ...或?干杯
我在使配置解析器模块工作时遇到了一些麻烦。我目前正在创建文件备份程序,但是每当我设置这些值时,它们都不会出现在 .ini 文件中。config.get 确认设置正确,但 document_backup.ini 为空。
config = configparser.ConfigParser()
config.read('document_backup.ini')
config.set('FTP_Login','Host',input("FTP Host: "))
config.set('FTP_Login','UserName',input("FTP UserName: "))
config.set('FTP_Login','Password',input("FTP Password: "))
config.read('') #To close the file, I think
Run Code Online (Sandbox Code Playgroud) 我正在config.ini为我的 python 项目编写一个文件。我想提一下列的标准日期格式。
像这样的东西
prod_db_end_date = 2017-01-01
prod_db_end_date_format = %Y-%m-%d
Run Code Online (Sandbox Code Playgroud)
但它会插入其他内容并出错。有人可以让我知道如何使用它吗?
Error traceback - File "C:\ProgramData\Anaconda3\lib\configparser.py", line 443, in _interpolate_some
"found: %r" % (rest,))
configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%Y-%m-%d'
Run Code Online (Sandbox Code Playgroud) 我有一个Python脚本,需要很多参数。我目前使用的是configuration.ini文件(使用读取configparser),但希望允许用户使用命令行覆盖特定的参数。如果我只有两个参数,我会使用类似的东西:
if not arg1:
arg1 = config[section]['arg1']
Run Code Online (Sandbox Code Playgroud)
但是我不想为30个参数做这些。有什么简单的方法可以从cmd行中获取可选参数,并将其默认设置为config文件?
import configparser
config= configparser.ConfigParser()
config.read(r'C:\Users\PycharmProjects\Integration\local.ini')
print(config.sections())
Run Code Online (Sandbox Code Playgroud)
不知道之后该做什么。我试过这段代码
server = config.get('db','server')
Run Code Online (Sandbox Code Playgroud)
它抛出 print 语句的输出和错误。
['"db"', '"Auth"']
configparser.NoSectionError: No section: 'db'
local.ini file contains
["db"]
server=raj
log=ere2
["Auth"]
login=hi
Run Code Online (Sandbox Code Playgroud) 使用模块“configparser”在python(3.7)中读取外部配置文件
示例配置文件“config.ini”
[ABC]
ch0 = "C:/Users/utility/ABC-ch0.txt"
ch1 = "C:/Users/utility/ABC-ch1.txt"
[settings]
script = "C:/Users/OneDrive/utility/xxxx.exe"
settings = "C:/Users/OneDrive/xxxxxxConfig.xml"
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的示例代码
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
ch0 = config.get('ABC','ch0')
print(ch0)
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误代码,不知道我哪里出错了
NoSectionError: No section: 'ABC'
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助.. 提前致谢
我想在配置文件中存储一些配置数据.这是一个示例部分:
[URLs]
Google, www.google.com
Hotmail, www.hotmail.com
Yahoo, www.yahoo.com
Run Code Online (Sandbox Code Playgroud)
是否可以使用ConfigParser模块将其读入元组列表?如果没有,我该怎么用?
我有问题要附加到配置文件.这就是我想要创造的东西;
[section1]
val1 = val2
val3 = val4
Run Code Online (Sandbox Code Playgroud)
但是当我运行以下代码时,我看到了 ConfigParser.NoSectionError: No section: 'section1'
import ConfigParser
cfg = ConfigParser.RawConfigParser()
cfg.set("section1", "val1", "val2")
f = open("example.cfg", "a")
cfg.write(f)
Run Code Online (Sandbox Code Playgroud)
如果我加
if not cfg.has_section("section1"):
cfg.add_section("section1")
Run Code Online (Sandbox Code Playgroud)
然后,这就是我得到的;
[section1]
val1 = val2
[section1]
val3 = val4
Run Code Online (Sandbox Code Playgroud)
有人能指出我做错了什么吗?谢谢
我正在编写一个新的RF库,需要使用字符串参数,因为我使用的(预先存在的)Python库需要字符串,而不是unicode.当然,我可以在调用仅支持字符串的现有函数之前将每个unicode转换为字符串.
import ConfigParser
class RFConfigParser:
def get (self,section, option):
print type (section) #prints unicode
section = str (section) #works but I dont want to do this
return self._config.get (section, option) #this pre-existing function expect a string input
Run Code Online (Sandbox Code Playgroud)
问题是我有很多类似的函数,在每个函数中我都要将这个unicode称为字符串转换马戏团.
有没有直接的方法这样做,因此RF功能将直接接受字符串格式
另一个问题是默认的unicode支持Robot Framework功能还是RIDE功能?(我正在使用RIDE,这就是我遇到这个问题的原因)
我有一个config.py,它读入多个配置文件.
from ConfigParser import *
def zones_config():
zones_config = ConfigParser()
zones = zones_config.sections()
zones_config.read('zones.ini'), zones
def settings_config():
settings_config = ConfigParser()
settings_config.read('settings.ini')
Run Code Online (Sandbox Code Playgroud)
然后在我的database.py文件中:
from utils.config import settings_config
settings_config()
#mysqbdb config
ip = settings_config.get('mysqldb', 'ip')
username = settings_config.get('mysqldb', 'username')
password = settings_config.get('mysqldb', 'password')
database = settings_config.get('mysqldb', 'database')
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
AttributeError: 'function' object has no attribute 'get'
Run Code Online (Sandbox Code Playgroud) 使用python configparser包读取配置文件时,所有键名都是小写字符串.有人知道如何读取保留大写和大写单词的字符串吗?
例如:
$cat config.cfg
[DEFAULT]
Key_1 = SomeWord
KEY_2 = Another Word
$ python3
>>> from configparser import ConfigParser
>>> cf = ConfigParser()
>>> cf.read('./config.cfg')
['./config.cfg']
>>> print(cf.defaults())
OrderedDict([('key_1', 'SomeWord'), ('key_2', 'Another Word')])
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!