configparser

How to ConfigParse a file keeping multiple values for identical keys?

做~自己de王妃 提交于 2019-12-28 12:09:12
问题 I need to be able to use the ConfigParser to read multiple values for the same key. Example config file: [test] foo = value1 foo = value2 xxx = yyy With the 'standard' use of ConfigParser there will be one key foo with the value value2 . But I need the parser to read in both values. Following an entry on duplicate key I have created the following example code: from collections import OrderedDict from ConfigParser import RawConfigParser class OrderedMultisetDict(OrderedDict): def __setitem__

pyodbc.OperationalError: ('HYT00', u'[HYT00] [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

家住魔仙堡 提交于 2019-12-24 08:17:13
问题 I am getting, self.cnxn = pyodbc.connect(self.connStr) pyodbc.OperationalError: ('HYT00', u'[HYT00] [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)') while connecting to sql-server database.I am reading the server information,username,password of the database from the .ini file.But i am not able to connect to the db.Can anyone help me to resolve this?(Refer the Below Code) def connectDB(self): config = configparser.ConfigParser() config.read(

How do I put a semicolon in a value in python configparser?

旧时模样 提交于 2019-12-24 02:23:19
问题 I need to specify a password on the right side of the equals sign in a python configparser file, but semicolon is the comment character. Escaping with \ does not work. How can I pass the string "foo;" as a value in configparser? 回答1: A short interactive session shows the semicolon is read without trouble. >>> import StringIO >>> import ConfigParser >>> f = StringIO.StringIO("[sec1]\npwd=foo;\n") >>> p = ConfigParser.ConfigParser() >>> p.readfp(f) >>> p.items('sec1') [('pwd', 'foo;')] >>> 回答2:

How to get case-sensitivity in Section names?

笑着哭i 提交于 2019-12-24 02:23:14
问题 I've got an win32 app accessed by users via RDP acess. Each user has his/her own user_app.ini file. When I upgrade my app on the RDP server, I sometimes need to create/modify entries in the user_app.ini file of each user. I wrote a Python script to handle the job that use some upgrade.ini file to update all the user_app.ini files, using the ConfigParser module. My problem is that my section names should be seen as case insensitive, but ConfigParser is case-sensitive regarding sections (while

MissingSectionHeaderError: File contains no section headers.(configparser)

。_饼干妹妹 提交于 2019-12-24 01:46:09
问题 I'm using configparser in order to read and modify automatically a file conf named 'streamer.conf'. I'm doing this : import configparser config = configparser.ConfigParser() config.read('C:/Users/../Desktop/streamer.conf') And then it breaks apart with this Error message : MissingSectionHeaderError: File contains no section headers. file: 'C:/Users/../Desktop/streamer.conf', line: 1 u'input{\n' What might be wrong? Any help appreciated. 回答1: just specify right encoding config.read(config_file

SafeConfigParser: sections and environment variables

僤鯓⒐⒋嵵緔 提交于 2019-12-24 01:20:05
问题 ( Using Python 3.4.3 ) I want to use environment variables in my config file and I read that I should use SafeConfigParser with os.environ as parameter to achieve it. [test] mytest = %(HOME)s/.config/my_folder Since I need to get all the options in a section, I am executing the following code: userConfig = SafeConfigParser(os.environ) userConfig.read(mainConfigFile) for section in userConfig.sections(): for item in userConfig.options(section): print( "### " + section + " -> " + item) My

How can I remove the white characters from configuration file?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 19:07:14
问题 I would like to modify the samba configuration file using python. This is my code 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 ) but the configuration file contains tab, is there any possibility to ignore the tabs? ConfigParser.ParsingError: File contains parsing errors: /etc/samba/smb.conf [line 38]: '

Mocking Method Calls In Python

风格不统一 提交于 2019-12-22 05:09:48
问题 I have been searching stack exchange and around the web for how to do this, but I cannot understand how to mock behaviors for methods. I am trying to mock openpyxl behaviors and behaviors for my custom class. Here is my attempt: import unittest from unittest.mock import MagicMock import openpyxl from MyPythonFile import MyClass class TestMyClass(unittest.TestCase): def test_myclass(self): myclass = MyClass() wb = openpyxl.workbook() ws = openpyxl.worksheet() wbPath = 'wbPath' openpyxl.load

Export with alphabetical sort in Python ConfigParser

你离开我真会死。 提交于 2019-12-21 12:30:13
问题 Is there any solution to force the RawConfigParser.write() method to export the config file with an alphabetical sort? Even if the original/loaded config file is sorted, the module mixes the section and the options into the sections arbitrarily, and is really annoying to edit manually a huge unsorted config file. PD: I'm using python 2.6 回答1: Three solutions: Pass in a dict type (second argument to the constructor) which returns the keys in your preferred sort order. Extend the class and

Python ConfigParser interpolation from foreign section

白昼怎懂夜的黑 提交于 2019-12-20 17:38:57
问题 With Python ConfigParser, is it possible to use interpolation across foreign sections? My mind seems to tell me I've seen that it's possible somewhere, but I can't find it when searching. This example doesn't work, but it's to give an idea of what I'm trying to do. [section1] root = /usr [section2] root = /usr/local [section3] dir1 = $(section1:root)/bin dir2 = $(section2:root)/bin Note that I'm using Python 2.4. 回答1: In python 3.2 and up this is perfectly valid: [Common] home_dir: /Users