New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
configparser unable to write comment with a upper cas letter #75716
Comments
I create entry with this: self.settings.set('General', 'Initial filter', 'All file (.*)') and after writing in a file: (; or # to test if differ) It is normal or not? Thank & Best Regards |
All config options are converted to lowercase when they are stored. You can customise this with https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.optionxform. You can customize it more with https://docs.python.org/3/library/configparser.html#customizing-parser-behaviour
>>> from configparser import ConfigParser
>>> c = ConfigParser()
>>> c["A"] = {'FOO': 'bar'}
>>> with open('example.ini', 'w') as configfile: c.write(configfile)
...
>>> with open('example.ini', 'r') as configfile: print(configfile.read())
...
[A]
foo = bar # Don't convert to lower case >>> d = ConfigParser()
>>> d.optionxform = str
>>> d["A"] = {'FOO': 'bar'}
>>> with open('example_case.ini', 'w') as configfile: d.write(configfile)
...
>>> with open('example_case.ini', 'r') as configfile: print(configfile.read())
...
[A]
FOO = bar Hope this answers your question. Feel free to close this if it's clear. Thanks |
Ah sorry, didn't notice it was about comments. It seems config.optionxform = str has no effect on comments. Thanks |
Ah my bad again. The config.optionxform = str does the trick. I was using an older object. from configparser import ConfigParser
config = ConfigParser(allow_no_value=True)
config.optionxform = str
config.add_section('default_settings')
config.set('default_settings', '; comment HERE')
with open('example_case.ini', 'w') as configfile: config.write(configfile)
with open('example_case.ini', 'r') as configfile: print(configfile.read()) Thanks |
All config options including comment are converted to lowercase when they are stored. You can customize this behavior using https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.optionxform . You can also refer to https://docs.python.org/3/library/configparser.html#customizing-parser-behaviour for more customization. I am closing this as not a bug as part of triaging. Feel free to reopen this if needed. Thanks for the report Philippe! |
Thank for your support. Sorry I have no time to give you an answer and work on with Python, but Best Regards Philippe Wagnières Le 24.09.2018 à 17:42, Karthikeyan Singaravelan a écrit :
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: