如何在部门名称中区分大小写? [英] How to get case-sensitivity in Section names?

查看:161
本文介绍了如何在部门名称中区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Win32应用程序,用户可以通过RDP访问访问。
每个用户都有他/她自己的 user_app.ini 文件。

I've got an win32 app accessed by users via RDP acess. Each user has his/her own user_app.ini file.

当我在在RDP服务器上,有时我需要在每个用户的 user_app.ini 文件中创建/修改条目。我编写了一个Python脚本来处理这项工作,该脚本使用一些 upgrade.ini 文件来更新所有 user_app.ini 文件,使用 ConfigParser 模块。

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.

我的问题是,我的节名应该不区分大小写,但是 ConfigParser 区分大小写(而对于 options 使用 optionxform可能不区分大小写()

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

推荐答案

有一个 SECTCRE 属性,您可以对其进行覆盖。这应该定义一个与节名匹配的正则表达式。

There is an SECTCRE attribute that you can override. This should define a regular expression that will match the section name.

代替正则表达式,您可以传递具有 match 方法的任何对象,该方法采用字符串并返回对象其中具有 group 方法,该方法接受字符串'header'作为参数。

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.

例如:

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.

这篇关于如何在部门名称中区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆