ConfigParser python 2.7. Delimiter is getting changed to '=' after config write
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
add a comment |
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
TheConfigParser
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 subclassConfigParser
, overwritewrite
and replace the=
in this line with a:
.
– Klaus D.
Nov 14 '18 at 7:25
add a comment |
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
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
python python-2.7 configparser
edited Nov 14 '18 at 7:17
Oluwafemi Sule
11.6k1532
11.6k1532
asked Nov 14 '18 at 7:11
SagarSagar
1891213
1891213
TheConfigParser
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 subclassConfigParser
, overwritewrite
and replace the=
in this line with a:
.
– Klaus D.
Nov 14 '18 at 7:25
add a comment |
TheConfigParser
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 subclassConfigParser
, overwritewrite
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
add a comment |
1 Answer
1
active
oldest
votes
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.
Thank you. This solution worked for me.
– Sagar
Nov 15 '18 at 12:45
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Thank you. This solution worked for me.
– Sagar
Nov 15 '18 at 12:45
add a comment |
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.
Thank you. This solution worked for me.
– Sagar
Nov 15 '18 at 12:45
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
, overwritewrite
and replace the=
in this line with a:
.– Klaus D.
Nov 14 '18 at 7:25