ConfigParser python 2.7. Delimiter is getting changed to '=' after config write










0















I have a file looks like below



[SectionOne]
Status: Single
Name: Derek
Value: Yes
Age: 30
Single: True


After i read and modify a field, the delimiter is getting changed to '=' instead of ':' as below



[SectionOne]
Status = Married
Name = Derek
Value = Yes
Age = 30
Single = True


I am using python 2.7 and i cannot migrate to a new version of python now.



the code is below



Config = ConfigParser.ConfigParser()
Config.read("Bacpypes.ini")
cfgfile = open("Bacpypes.ini")
Config.set('SectionOne', 'Status', 'Married')
Config.write(cfgfile
cfgfile.close()


Thanks in advance










share|improve this question
























  • The ConfigParser in Python 3 allows defining the "delimiters". The version in Python 2 does not.

    – Klaus D.
    Nov 14 '18 at 7:18











  • ...but you could subclass ConfigParser, overwrite write and replace the = in this line with a :.

    – Klaus D.
    Nov 14 '18 at 7:25
















0















I have a file looks like below



[SectionOne]
Status: Single
Name: Derek
Value: Yes
Age: 30
Single: True


After i read and modify a field, the delimiter is getting changed to '=' instead of ':' as below



[SectionOne]
Status = Married
Name = Derek
Value = Yes
Age = 30
Single = True


I am using python 2.7 and i cannot migrate to a new version of python now.



the code is below



Config = ConfigParser.ConfigParser()
Config.read("Bacpypes.ini")
cfgfile = open("Bacpypes.ini")
Config.set('SectionOne', 'Status', 'Married')
Config.write(cfgfile
cfgfile.close()


Thanks in advance










share|improve this question
























  • The ConfigParser in Python 3 allows defining the "delimiters". The version in Python 2 does not.

    – Klaus D.
    Nov 14 '18 at 7:18











  • ...but you could subclass ConfigParser, overwrite write and replace the = in this line with a :.

    – Klaus D.
    Nov 14 '18 at 7:25














0












0








0








I have a file looks like below



[SectionOne]
Status: Single
Name: Derek
Value: Yes
Age: 30
Single: True


After i read and modify a field, the delimiter is getting changed to '=' instead of ':' as below



[SectionOne]
Status = Married
Name = Derek
Value = Yes
Age = 30
Single = True


I am using python 2.7 and i cannot migrate to a new version of python now.



the code is below



Config = ConfigParser.ConfigParser()
Config.read("Bacpypes.ini")
cfgfile = open("Bacpypes.ini")
Config.set('SectionOne', 'Status', 'Married')
Config.write(cfgfile
cfgfile.close()


Thanks in advance










share|improve this question
















I have a file looks like below



[SectionOne]
Status: Single
Name: Derek
Value: Yes
Age: 30
Single: True


After i read and modify a field, the delimiter is getting changed to '=' instead of ':' as below



[SectionOne]
Status = Married
Name = Derek
Value = Yes
Age = 30
Single = True


I am using python 2.7 and i cannot migrate to a new version of python now.



the code is below



Config = ConfigParser.ConfigParser()
Config.read("Bacpypes.ini")
cfgfile = open("Bacpypes.ini")
Config.set('SectionOne', 'Status', 'Married')
Config.write(cfgfile
cfgfile.close()


Thanks in advance







python python-2.7 configparser






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 7:17









Oluwafemi Sule

11.6k1532




11.6k1532










asked Nov 14 '18 at 7:11









SagarSagar

1891213




1891213












  • The ConfigParser in Python 3 allows defining the "delimiters". The version in Python 2 does not.

    – Klaus D.
    Nov 14 '18 at 7:18











  • ...but you could subclass ConfigParser, overwrite write and replace the = in this line with a :.

    – Klaus D.
    Nov 14 '18 at 7:25


















  • The ConfigParser in Python 3 allows defining the "delimiters". The version in Python 2 does not.

    – Klaus D.
    Nov 14 '18 at 7:18











  • ...but you could subclass ConfigParser, overwrite write and replace the = in this line with a :.

    – Klaus D.
    Nov 14 '18 at 7:25

















The ConfigParser in Python 3 allows defining the "delimiters". The version in Python 2 does not.

– Klaus D.
Nov 14 '18 at 7:18





The ConfigParser in Python 3 allows defining the "delimiters". The version in Python 2 does not.

– Klaus D.
Nov 14 '18 at 7:18













...but you could subclass ConfigParser, overwrite write and replace the = in this line with a :.

– Klaus D.
Nov 14 '18 at 7:25






...but you could subclass ConfigParser, overwrite write and replace the = in this line with a :.

– Klaus D.
Nov 14 '18 at 7:25













1 Answer
1






active

oldest

votes


















1














Try to subclass the ConfigParser to modify its behaviour so it writes a : instead of =:



class MyConfigParser(ConfigParser.ConfigParser):

def write(self, fp):
"""Write an .ini-format representation of the configuration state."""
if self._defaults:
fp.write("[%s]n" % DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write("%s = %sn" % (key, str(value).replace('n', 'nt')))
fp.write("n")
for section in self._sections:
fp.write("[%s]n" % section)
for (key, value) in self._sections[section].items():
if key == "__name__":
continue
if (value is not None) or (self._optcre == self.OPTCRE):
key = ": ".join((key, str(value).replace('n', 'nt')))
fp.write("%sn" % (key))
fp.write("n")


Then use MyConfigParser:



config = MyConfigParser()
config.read("Bacpypes.ini")
...


In addition to that there are two errors in your code:



  • You did not open the file for writing.


  • You have unbalanced parentheses.


Both should prevent the code from running.






share|improve this answer























  • Thank you. This solution worked for me.

    – Sagar
    Nov 15 '18 at 12:45










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294838%2fconfigparser-python-2-7-delimiter-is-getting-changed-to-after-config-write%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Try to subclass the ConfigParser to modify its behaviour so it writes a : instead of =:



class MyConfigParser(ConfigParser.ConfigParser):

def write(self, fp):
"""Write an .ini-format representation of the configuration state."""
if self._defaults:
fp.write("[%s]n" % DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write("%s = %sn" % (key, str(value).replace('n', 'nt')))
fp.write("n")
for section in self._sections:
fp.write("[%s]n" % section)
for (key, value) in self._sections[section].items():
if key == "__name__":
continue
if (value is not None) or (self._optcre == self.OPTCRE):
key = ": ".join((key, str(value).replace('n', 'nt')))
fp.write("%sn" % (key))
fp.write("n")


Then use MyConfigParser:



config = MyConfigParser()
config.read("Bacpypes.ini")
...


In addition to that there are two errors in your code:



  • You did not open the file for writing.


  • You have unbalanced parentheses.


Both should prevent the code from running.






share|improve this answer























  • Thank you. This solution worked for me.

    – Sagar
    Nov 15 '18 at 12:45















1














Try to subclass the ConfigParser to modify its behaviour so it writes a : instead of =:



class MyConfigParser(ConfigParser.ConfigParser):

def write(self, fp):
"""Write an .ini-format representation of the configuration state."""
if self._defaults:
fp.write("[%s]n" % DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write("%s = %sn" % (key, str(value).replace('n', 'nt')))
fp.write("n")
for section in self._sections:
fp.write("[%s]n" % section)
for (key, value) in self._sections[section].items():
if key == "__name__":
continue
if (value is not None) or (self._optcre == self.OPTCRE):
key = ": ".join((key, str(value).replace('n', 'nt')))
fp.write("%sn" % (key))
fp.write("n")


Then use MyConfigParser:



config = MyConfigParser()
config.read("Bacpypes.ini")
...


In addition to that there are two errors in your code:



  • You did not open the file for writing.


  • You have unbalanced parentheses.


Both should prevent the code from running.






share|improve this answer























  • Thank you. This solution worked for me.

    – Sagar
    Nov 15 '18 at 12:45













1












1








1







Try to subclass the ConfigParser to modify its behaviour so it writes a : instead of =:



class MyConfigParser(ConfigParser.ConfigParser):

def write(self, fp):
"""Write an .ini-format representation of the configuration state."""
if self._defaults:
fp.write("[%s]n" % DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write("%s = %sn" % (key, str(value).replace('n', 'nt')))
fp.write("n")
for section in self._sections:
fp.write("[%s]n" % section)
for (key, value) in self._sections[section].items():
if key == "__name__":
continue
if (value is not None) or (self._optcre == self.OPTCRE):
key = ": ".join((key, str(value).replace('n', 'nt')))
fp.write("%sn" % (key))
fp.write("n")


Then use MyConfigParser:



config = MyConfigParser()
config.read("Bacpypes.ini")
...


In addition to that there are two errors in your code:



  • You did not open the file for writing.


  • You have unbalanced parentheses.


Both should prevent the code from running.






share|improve this answer













Try to subclass the ConfigParser to modify its behaviour so it writes a : instead of =:



class MyConfigParser(ConfigParser.ConfigParser):

def write(self, fp):
"""Write an .ini-format representation of the configuration state."""
if self._defaults:
fp.write("[%s]n" % DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write("%s = %sn" % (key, str(value).replace('n', 'nt')))
fp.write("n")
for section in self._sections:
fp.write("[%s]n" % section)
for (key, value) in self._sections[section].items():
if key == "__name__":
continue
if (value is not None) or (self._optcre == self.OPTCRE):
key = ": ".join((key, str(value).replace('n', 'nt')))
fp.write("%sn" % (key))
fp.write("n")


Then use MyConfigParser:



config = MyConfigParser()
config.read("Bacpypes.ini")
...


In addition to that there are two errors in your code:



  • You did not open the file for writing.


  • You have unbalanced parentheses.


Both should prevent the code from running.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 7:34









Klaus D.Klaus D.

7,61811936




7,61811936












  • Thank you. This solution worked for me.

    – Sagar
    Nov 15 '18 at 12:45

















  • Thank you. This solution worked for me.

    – Sagar
    Nov 15 '18 at 12:45
















Thank you. This solution worked for me.

– Sagar
Nov 15 '18 at 12:45





Thank you. This solution worked for me.

– Sagar
Nov 15 '18 at 12:45



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294838%2fconfigparser-python-2-7-delimiter-is-getting-changed-to-after-config-write%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto