【问题标题】:how to read hashtags in python?如何在 python 中读取主题标签?
【发布时间】:2021-09-03 02:16:41
【问题描述】:

我正在尝试读取如下配置文件:

[servers]
    [germany]
        test=1
        safd=134i
    [at]
        ip=asfd

我正在尝试解析配置

import configparser

p = configparser.ConfigParser()

print ("p.read: \t", p.read('config.cfg'))
print ("secions: \t", p.sections())
for i in p.sections():
    print(i)

输出:

p.read:          ['config.cfg']
secions:         ['servers', 'germany', 'at']
servers
germany
at

我想浏览这些部分和子部分,例如

for i in p.sections(): 
  print(i.[0][1]) 

或类似的东西。

配置文件错了吗?我的意思是我该如何处理 在 python 中?

谢谢。

【问题讨论】:

    标签: python config-files


    【解决方案1】:

    The configparser module is for parsing INI filesINI files are not hierarchical,所以配置文件中的缩进被简单地忽略了。

    如果您可以控制配置文件的格式,最好使用JSONYAML

    可以使用标准库中的 json 模块解析 JSON。

    可以使用第 3 方 pyyaml 包(您需要使用 pip 安装)解析 YAML,它具有与 json 模块类似的 API,并且可以使用 import yaml 导入。

    【讨论】:

      猜你喜欢
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      相关资源
      最近更新 更多