问题
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 it can be case insensitve regarding options using optionxform()
)
Can someone help me ? Thanks
回答1:
There is an SECTCRE attribute that you can override. This should define a regular expression that will match the section name.
Instead of a regex you can pass any object that has a match
method which takes a string and returns an object which has a group
method that accepts the string 'header'
as parameter.
For example:
class FakeRe:
def __init__(self, regex):
self.regex = regex
def match(self, text):
m = self.regex.match(text)
if m:
return FakeMatch(m)
return None
class FakeMatch:
def __init__(self, match):
self.match = match
def group(self, name):
return self.match.group(name).lower()
You could then set that attribute when creating a parser:
config = ConfigParser()
config.SECTCRE = FakeRe(re.compile(r'\[\s*(?P<header>some regex here)\s*\]')
and this parser should consider all section names as lower-case.
You may have to tweak the code a bit, depending on your exact needs.
来源:https://stackoverflow.com/questions/26179728/how-to-get-case-sensitivity-in-section-names