python ini文件删除修改_如何在INI文件中编写时删除空格 - Python
這是RawConfigParser.write的定義:
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 = %s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")
for section in self._sections:
fp.write("[%s]\n" % section)
for (key, value) in self._sections[section].items():
if key != "__name__":
fp.write("%s = %s\n" %
(key, str(value).replace('\n', '\n\t')))
fp.write("\n")如您所見,%s = %s\n格式被硬編碼到函數中。我認為你的選擇是:
使用帶有等于的空格的INI文件
用您自己的覆蓋RawConfigParser的write方法
編寫文件,讀取文件,刪除空格,然后重新編寫
如果你100%確定選項1不可用,這里有一個方法可以做選項3:
def remove_whitespace_from_assignments():
separator = "="
config_path = "config.ini"
lines = file(config_path).readlines()
fp = open(config_path, "w")
for line in lines:
line = line.strip()
if not line.startswith("#") and separator in line:
assignment = line.split(separator, 1)
assignment = map(str.strip, assignment)
fp.write("%s%s%s\n" % (assignment[0], separator, assignment[1]))
else:
fp.write(line + "\n")
總結
以上是生活随笔為你收集整理的python ini文件删除修改_如何在INI文件中编写时删除空格 - Python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3怎么做爬虫_Python爬
- 下一篇: flask get 参数_用它 5 分钟