【问题标题】:Can python read this type of config file?python可以读取这种类型的配置文件吗?
【发布时间】:2020-02-03 17:20:36
【问题描述】:

早上好。我要你帮忙。我正在使用 python 制作一个项目,但我想知道 python2 是否可以读取这种类型的配置文件。示例:

[SETTINGS]
{
  "Name": "SKY",
  "SuggestedBots": 50,
  "MaxCPM": 3000,
  "LastModified": "2019-11-03T23:24:24.0854425-03:00",
  "AdditionalInfo": "",
  "Author": "KATO",
  "Version": "1.1.4",
  "IgnoreResponseErrors": false,
  "MaxRedirects": 8,
  "NeedsProxies": true,
  "OnlySocks": false,
  "OnlySsl": false,
  "MaxProxyUses": 0,
  "BanProxyAfterGoodStatus": false,
  "EncodeData": false,
  "AllowedWordlist1": "",
  "AllowedWordlist2": "",
  "DataRules": [],
  "CustomInputs": [],
  "ForceHeadless": false,
  "AlwaysOpen": false,
  "AlwaysQuit": false,
  "DisableNotifications": false,
  "CustomUserAgent": "",
  "RandomUA": false,
  "CustomCMDArgs": ""
}

我尝试了互联网上的教程,但没有用。我认为是 JSON。

【问题讨论】:

  • 那个 JSON/ini 混合可憎的东西是从哪里来的?
  • 第一行不符合 JSON,它看起来像一个名字。如果您只是删除文件的第一行,则 json 内置库应该能够处理其余部分。
  • 如果你想在没有外部库的情况下这样做,与stackoverflow.com/q/59831811 非常相似。
  • 谢谢大家,你们帮帮我:)。

标签: python json python-2.7 file config


【解决方案1】:

如果你去掉第一行,那么它可以在 python 中使用。

import json

test = '''{
  "Name": "SKY",
  "SuggestedBots": 50,
  "MaxCPM": 3000,
  "LastModified": "2019-11-03T23:24:24.0854425-03:00",
  "AdditionalInfo": "",
  "Author": "KATO",
  "Version": "1.1.4",
  "IgnoreResponseErrors": false,
  "MaxRedirects": 8,
  "NeedsProxies": true,
  "OnlySocks": false,
  "OnlySsl": false,
  "MaxProxyUses": 0,
  "BanProxyAfterGoodStatus": false,
  "EncodeData": false,
  "AllowedWordlist1": "",
  "AllowedWordlist2": "",
  "DataRules": [],
  "CustomInputs": [],
  "ForceHeadless": false,
  "AlwaysOpen": false,
  "AlwaysQuit": false,
  "DisableNotifications": false,
  "CustomUserAgent": "",
  "RandomUA": false,
  "CustomCMDArgs": ""
}'''

json.loads(test)

# {u'AlwaysQuit': False, u'Author': u'KATO', u'LastModified': u'2019-11-03T23:24:24.0854425-03:00', u'DataRules': [], u'AlwaysOpen': False, u'Version': u'1.1.4', u'DisableNotifications': False, u'NeedsProxies': True, u'CustomInputs': [], u'EncodeData': False, u'BanProxyAfterGoodStatus': False, u'SuggestedBots': 50, u'ForceHeadless': False, u'RandomUA': False, u'AdditionalInfo': u'', u'Name': u'SKY', u'CustomUserAgent': u'', u'MaxRedirects': 8, u'CustomCMDArgs': u'', u'OnlySocks': False, u'MaxProxyUses': 0, u'IgnoreResponseErrors': False, u'AllowedWordlist1': u'', u'AllowedWordlist2': u'', u'OnlySsl': False, u'MaxCPM': 3000}

【讨论】:

  • 谢谢兄弟,对我帮助很大。
猜你喜欢
  • 2021-02-23
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
  • 2013-04-03
  • 2013-01-04
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多