- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 python 3.4,我在以格式化方式读取具有 json 格式的配置文件时遇到问题(即不在同一行中。)我的配置文件“Example.config”如下所示
[ABC]
outdoor = {'GAME':['cricket',
'football'],
'INFO':[
{'Players':'eleven',
'Fun':'Unlimited'},
{'Players':'something',
'Fun':'Much_more'}
]
}
[XYZ]
INDOOR = {'GAME':['Carom'],
'INFO':[
{'Players':'2','Fun':'Less'},
{'Players':'4','Fun':'More'}
]
}
INDOOR1 = {'GAME':['TABLE-TENNIS',
'BASKETBALL',
'ANYTHING'],
'INFO':[
{'Players':'TWO','Fun':'MORE'},
{'Players':'MORE','Fun':'MUCHMORE'}
]
}
我的代码
config_parser=ConfigParser()
config_parser.read('Example.config')
for each_section in config_parser.sections():
for option in config_parser.options(each_section):
option=ast.literal_eval(option)
try:
games=option['GAME']
info=option['INFO']
except:
print('PLease format config same as qa_config.config file.Please Pay special attention to casing')
raise
for game in games:
for eachInfo in info:
_INFO=eachInfo['Players']
_FUN=eachInfo['Fun']
print (each_section,':\n')
print ('\t',game,':\n')
print ('\t',_INFO,':\n')
print ('\t',_FUN,':\n')
错误:
option=ast.literal_eval(option)
File "C:\Python34\lib\ast.py", line 84, in literal_eval
return _convert(node_or_string)
File "C:\Python34\lib\ast.py", line 83, in _convert
raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Name object at 0x02E61910>
如果我将字典作为字符串保留在一行中,而不在配置文件中进行任何美化 ast.literal_eval()
就能够处理它。
例如:
outdoor = {'GAME':['cricket','football'],'INFO':[{'Players':'eleven','Fun':'Unlimited'},{'Players':'something','Fun':'Much_more'}]}
所以我猜这是由于我在配置文件中使用的制表符和空格造成的。但我需要美化配置文件,因为用户会更新它。
我还尝试过 asteval
和 json.load
。 T.I.A.
最佳答案
我尝试了你的代码和示例(诚然在 2.7 中)和问题是您在错误的事情上使用了 ast.literal_eval()
。
您在选项
上使用它,这是左侧的名称ini 文件(在您的示例中为“outdoor”、“INDOOR”和“INDOOR1”)。您想在此选项的右侧值使用它。
将下面缺少的第二行添加到脚本中的相应循环中:
for option in config_parser.options(each_section):
option = config_parser.get(each_section, option)
option = ast.literal_eval(option)
您的多行 ini 文件已完美解析。
关于python - 如何在python中读取json嵌入ini配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31746977/
PHP 解释器是否首先加载它找到的 php.ini(根据其搜索算法)并停止。还是加载整个 php.ini 层次结构并合并设置? 因此,如果我需要覆盖单个网站的单个设置 - 我需要指定所有值,还是只需要
关于以下形式的归并排序算法: public static void merge(int [] a, int ini, int med, int end){ int [] b = new
如何从另一个 ini 文件解析变量? 在下面的例子中,我想解析 var_a来自 original_configuration.ini并在 new_configuration.ini 使用它 origi
我无法获取 ini 文件中数组的值。 这是 ini 文件: module.name = Core module.version = 1 module.package = 'Core Modules'
编辑 eclipse.ini (OS Ubuntu 12.04 LTS) 后无法保存。如何在 eclipse.ini 文件或任何其他 .ini 文件中进行更改? Eclipse 和 Scala IDE
我目前正在使用 Zend Framework 从事个人项目。我一直无法获得良好的搜索结果,也没有问过我的同事:-/。 我在工作中经常使用 Ant/Phing,你有一个叫做“属性文件”的东西,它们有这个
我想提高我的 mysql 性能。所以我尝试使用 my-large.ini。 首先,我将该文件重命名为 my.ini。然后我在 [mysqld] 下添加了以下行 port=3306 basedir="C
我似乎遇到了一个问题,但感觉它不是一个问题,但我看不到解决方案,所以也许其他人可以。 我正在使用 ini 文件来存储我正在编写的包的配置详细信息。此 ini 文件包含与其他 ini 文件相关的部分。
我正在使用 Java API ini4j解析 ini 文件。我的原始 .ini 文件具有以下键值格式: [section] key=value = 字符周围没有空格。 但是在使用 ini.store(
我有写一段 ini 文件的函数: boolean saveSSVar() { using boost::property_tree::ptree; ptree pt; pt.p
最近我在 ubuntu 16.04 机器上安装了 lamp 和 php-xdebug。我注意到现在我有以下文件 /etc/php/7.0/apache2/conf.d/20-xdebug.ini /e
我正在尝试为我的 Pyramid 项目配置 SQLAlchemy Alembic,我想使用我的 developement.ini(或 production.ini)来配置 Alembic。是否可以指定
我的 FlashBuilder 实例在进行一些繁重的分析时不断崩溃。 必须说,我也在生成对象分配跟踪(全部,而不是默认的 10 个)并大量遍历 GC 路径。 但是我有一种预感,我可以通过允许 Flas
我是网络世界的新手,想知道当服务器默认 php.ini 文件时,自定义 php.ini(我们手动创建并添加到每个目录) 文件,.user.ini 文件由服务器加载/读取? 根据我的概念,这些文件在每次
在创建组件时,我在Joomla的组件目录结构中看到了两个语言文件:.ini和 sys.ini .有什么不同? 最佳答案 虽然@Lodder 本质上是正确的 sys.ini文件实际上有 3 个角色。 它
是否可以加载一个自定义的 .ini 文件来覆盖分发包中乱七八糟的 php.ini? 在 unix 系统上,我相信这可以通过在 /etc/php.d 中放置额外的 ini 文件来实现,但我不确定 IIS
如何在另一个 php.ini 文件中包含一个 php.ini 文件? 最佳答案 我认为您不能从主 php.ini 文件中“包含”.ini 文件。 不过,一个可能的解决方案可能是在编译 PHP 时在配置
您好,我有一个代码,其中使用了几个 INI 文件进行预定义设置。我可以在 INI 文件中使用#ifdef 吗?如果可以,我该如何使用它?如果不是,我如何限制我对 INI 文件的代码编译。例如我有一个宏
下面的代码片段可以编辑 ini 文件,但会将所有 ini 条目替换为小写: config = ConfigParser.RawConfigParser() config.read("test.ini"
我按照 this article 中的说明安装并运行 drush :我的服务器是cloudlinux和cagefs.drupal7 Drush 使用全局 php.ini 文件而不是 drish.ini
我是一名优秀的程序员,十分优秀!