This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: configparser unable to write comment with a upper cas letter
Type: enhancement Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: philippewagnieres@hispeed.ch, xtreak
Priority: normal Keywords:

Created on 2017-09-20 16:28 by philippewagnieres@hispeed.ch, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg302633 - (view) Author: Philippe Wagnieres (philippewagnieres@hispeed.ch) Date: 2017-09-20 16:28
I create entry with this:

   self.settings.set('General', 'Initial filter', 'All file (*.*)')
   self.settings.set('General', '# 1 => Text files (*.txt)')
   self.settings.set('General', '# 2 => CSV files (*.csv)')
   self.settings.set('General', '# 3 => Text files (*.txt) \n')

and after writing in a file:
   initial filter = All file (*.*)
   ; 1 => text files (*.txt)
   ; 2 => csv files (*.csv)
   # 3 => text files (*.txt) 

(; or # to test if differ)

It is normal or not?

Thank & Best Regards
msg325000 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-09-11 11:15
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

>  Note also that keys in sections are case-insensitive and stored in lowercase 

>>> 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
msg325002 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-09-11 11:34
Ah sorry, didn't notice it was about comments. It seems config.optionxform = str has no effect on comments.

Thanks
msg325003 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-09-11 11:38
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
msg326255 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-09-24 15:42
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!
msg326422 - (view) Author: Philippe Wagnieres (philippewagnieres@hispeed.ch) Date: 2018-09-26 07:15
Thank for your support.

Sorry I have no time to give you an answer and work on with Python, but 
I have understand the solution.

Best Regards

Philippe Wagnières
Chalamont 6
1400 Yverdon-les-Bains
Suisse
tel.: +41 76 367 27 43

Le 24.09.2018 à 17:42, Karthikeyan Singaravelan a écrit :
> Karthikeyan Singaravelan <tir.karthi@gmail.com> added the comment:
>
> 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!
>
> ----------
> resolution:  -> not a bug
> stage:  -> resolved
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue31535>
> _______________________________________
History
Date User Action Args
2022-04-11 14:58:52adminsetgithub: 75716
2018-09-26 07:15:50philippewagnieres@hispeed.chsetmessages: + msg326422
2018-09-24 15:42:26xtreaksetstatus: open -> closed
resolution: not a bug
messages: + msg326255

stage: resolved
2018-09-11 11:38:37xtreaksetmessages: + msg325003
2018-09-11 11:34:27xtreaksetmessages: + msg325002
2018-09-11 11:15:23xtreaksetmessages: + msg325000
2018-09-11 10:53:13xtreaksetnosy: + xtreak
2017-09-20 16:28:22philippewagnieres@hispeed.chcreate