gpt4 book ai didi

python - 来自 SUDS.client 的未知字符串格式(汤?)的可能解析器

转载 作者:行者123 更新时间:2023-12-01 05:31:33 26 4
gpt4 key购买 nike

我正在使用 suds 包从网站查询 API,从他们的网站返回的数据如下所示:

(1)。谁能告诉我这是什么格式?

(2)。如果是这样,解析数据的最简单方法是什么?我已经使用 BeautifulSoup 处理过很多 HTML/XML 格式,但在我为这种格式编写正则表达式之前。我很好奇这是某种类型的“流行格式”,实际上已经编写了一些漂亮的解析器。谢谢。

    # Below are the header and tail of the response.. 
(DetailResult)
{

status = (Status){ message = None code = "0" }

searchArgument = (DetailSearchArgument){ reqPartNumber = "BQ" reqMfg = "T" reqCpn = None }

detailsDto[] = (DetailsDto){

summaryDto = (SummaryDto){ PartNumber = "BQ" seMfg = "T" description = "Fast" }

packageDto[] =
(PackageDto){ fetName = "a" fetValue = "b" },
(PackageDto){ fetName = "c" fetValue = "d" },
(PackageDto){ fetName = "d" fetValue = "z" },
(PackageDto){ fetName = "f" fetValue = "Sq" },
(PackageDto){ fetName = "g" fetValue = "p" },

additionalDetailsDto = (AdditionalDetailsDto){ cr = None pOptions = None inv = None pcns = None }

partImageDto = None

riskDto = (RiskDto){ life= "Low" lStage = "Mature" yteol = "10" Date = "2023"}

partOptionsDto[] = (ReplacementDto){ partNumber = "BQ2" manufacturer = "T" type = "Reel" },

inventoryDto[] =
(InventoryDto){ distributor = "V" quantity = "88" buyNowLink = "https://www..." },
(InventoryDto){ distributor = "R" quantity = "7" buyNowLink = "http://www.r." },
(InventoryDto){ distributor = "RS" quantity = "2" buyNowLink = "http://www.rs.." },
},

}

最佳答案

这看起来像是某种嵌套的 repr 输出,类似于 JSON,但带有结构或对象名称信息(“状态包含消息和代码”)。如果是嵌套的,仅靠正则表达式无法完成这项工作。这是 pyparsing 的粗略过程解析器

sample = """
... given sample text ...
"""
from pyparsing import *

# punctuation
LPAR,RPAR,LBRACE,RBRACE,LBRACK,RBRACK,COMMA,EQ = map(Suppress,"(){}[],=")

identifier = Word(alphas,alphanums+"_")

# define some types that can get converted to Python types
# (parse actions will do conversion at parse time)
NONE = Keyword("None").setParseAction(replaceWith(None))
integer = Word(nums).setParseAction(lambda t:int(t[0]))
quotedString.setParseAction(removeQuotes)

# define a placeholder for a nested object definition (since objDefn
# will be referenced within its own definition)
objDefn = Forward()

objType = Combine(LPAR + identifier + RPAR)

objval = quotedString | NONE | integer | Group(objDefn)
objattr = Group(identifier + EQ + objval)

arrayattr = Group(identifier + LBRACK + RBRACK + EQ + Group(OneOrMore(Group(objDefn)+COMMA)) )

# use '<<' operator to assign content to previously declared Forward
objDefn << objType + LBRACE + ZeroOrMore((arrayattr | objattr) + Optional(COMMA)) + RBRACE
# parse sample text
result = objDefn.parseString(sample)

# use pprint to list out indented parsed data
import pprint
pprint.pprint(result.asList())

打印:

['DetailResult',
['status', ['Status', ['message', None], ['code', '0']]],
['searchArgument',
['DetailSearchArgument',
['reqPartNumber', 'BQ'],
['reqMfg', 'T'],
['reqCpn', None]]],
['detailsDto',
[['DetailsDto',
['summaryDto',
['SummaryDto',
['PartNumber', 'BQ'],
['seMfg', 'T'],
['description', 'Fast']]],
['packageDto',
[['PackageDto', ['fetName', 'a'], ['fetValue', 'b']],
['PackageDto', ['fetName', 'c'], ['fetValue', 'd']],
['PackageDto', ['fetName', 'd'], ['fetValue', 'z']],
['PackageDto', ['fetName', 'f'], ['fetValue', 'Sq']],
['PackageDto', ['fetName', 'g'], ['fetValue', 'p']]]],
['additionalDetailsDto',
['AdditionalDetailsDto',
['cr', None],
['pOptions', None],
['inv', None],
['pcns', None]]],
['partImageDto', None],
['riskDto',
['RiskDto',
['life', 'Low'],
['lStage', 'Mature'],
['yteol', '10'],
['Date', '2023']]],
['partOptionsDto',
[['ReplacementDto',
['partNumber', 'BQ2'],
['manufacturer', 'T'],
['type', 'Reel']]]],
['inventoryDto',
[['InventoryDto',
['distributor', 'V'],
['quantity', '88'],
['buyNowLink', 'https://www...']],
['InventoryDto',
['distributor', 'R'],
['quantity', '7'],
['buyNowLink', 'http://www.r.']],
['InventoryDto',
['distributor', 'RS'],
['quantity', '2'],
['buyNowLink', 'http://www.rs..']]]]]]]]

关于python - 来自 SUDS.client 的未知字符串格式(汤?)的可能解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20133824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com