【问题标题】:Preserve camel case and comments in config file when using ConfigParser使用 ConfigParser 时在配置文件中保留驼峰式大小写和注释
【发布时间】:2020-06-07 06:42:51
【问题描述】:

我正在使用ConfigParser 编辑我的配置文件,但在更新配置文件中的值时,它最终会删除所有 cmets 并将所有选项值从驼峰式转换为小写。

我的初始配置文件:

[Recording]

Record Video = Yes
Type = mp4 

# You can use [low/medium/high] all lowercase
Quality = medium

# The name of recording file.
Name = Recording

我的 Python 脚本

import configparser

CFG_FILENAME = 'config.txt'

cfg = configparser.RawConfigParser()
cfg.read(str(CFG_FILENAME))
cfgfile = open(str(CFG_FILENAME), 'w')
cfg.set('Recording', 'Record Video','No')
cfg.write(cfgfile)
cfgfile.close()

输出文件

[Recording]
record video = No
type = mp4
quality = medium
name = Recording

请帮我解决这个问题。提前致谢。

【问题讨论】:

    标签: python-3.x configparser


    【解决方案1】:

    设置此选项以保留大小写。 在这里查看Preserve case in ConfigParser?

    config.optionxform=str
    

    【讨论】:

      猜你喜欢
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 2014-10-22
      相关资源
      最近更新 更多