我有一堆嵌套数据,其格式类似于JSON:
company="My Company" phone="555-5555" people= { person= { name="Bob" location="Seattle" settings= { size=1 color="red" } } person= { name="Joe" location="Seattle" settings= { size=2 color="blue" } } } places= { ... }
有许多不同的参数具有不同的深度级别-这只是一个很小的子集。
还可能值得注意的是,当创建新的子数组时,总是有一个等号,后跟一个换行符,然后是一个空心括号(如上所示)。
是否有任何简单的循环或递归技术将此数据转换为系统友好的数据格式,例如数组或JSON?我想避免对属性名称进行硬编码。我正在寻找可以在Python,Java或PHP中使用的东西。伪代码也可以。
感谢您的帮助。
编辑:我发现了Python的Pyparsing库,它看起来可能是一个很大的帮助。我找不到任何有关如何使用Pyparsing解析未知深度的嵌套结构的示例。任何人都可以根据我上面描述的数据了解Pyparsing吗?
编辑2:好的,这是Pyparsing中的有效解决方案:
def parse_file(fileName): #get the input text file file = open(fileName, "r") inputText = file.read() #define the elements of our data pattern name = Word(alphas, alphanums+"_") EQ,LBRACE,RBRACE = map(Suppress, "={}") value = Forward() #this tells pyparsing that values can be recursive entry = Group(name + EQ + value) #this is the basic name-value pair #define data types that might be in the values real = Regex(r"[+-]?\d+\.\d*").setParseAction(lambda x: float(x[0])) integer = Regex(r"[+-]?\d+").setParseAction(lambda x: int(x[0])) quotedString.setParseAction(removeQuotes) #declare the overall structure of a nested data element struct = Dict(LBRACE + ZeroOrMore(entry) + RBRACE) #we will turn the output into a Dictionary #declare the types that might be contained in our data value - string, real, int, or the struct we declared value << (quotedString | struct | real | integer) #parse our input text and return it as a Dictionary result = Dict(OneOrMore(entry)).parseString(inputText) return result.dump()
这可行,但是当我尝试将结果写入带有json.dump(result)的文件时,文件的内容用双引号引起来。同样,\n许多数据对之间也有字符。我曾尝试在上面的代码中使用来抑制它们LineEnd().suppress(),但一定不能正确使用它。
\n
LineEnd().suppress()
好的,我想出了一个最终的解决方案,该解决方案实际上将数据转换为我最初想要的JSON友好型Dict。它首先使用Pyparsing将数据转换为一系列嵌套列表,然后循环遍历该列表并将其转换为JSON。这使我能够克服以下问题:Pyparsing的toDict()方法无法处理同一对象具有相同名称的两个属性的情况。为了确定列表是纯列表还是属性/值对,当Pyparsing检测到属性名称时,该prependPropertyToken方法会将字符串添加到__property__属性名称的前面。
toDict()
prependPropertyToken
__property__
def parse_file(self,fileName): #get the input text file file = open(fileName, "r") inputText = file.read() #define data types that might be in the values real = Regex(r"[+-]?\d+\.\d*").setParseAction(lambda x: float(x[0])) integer = Regex(r"[+-]?\d+").setParseAction(lambda x: int(x[0])) yes = CaselessKeyword("yes").setParseAction(replaceWith(True)) no = CaselessKeyword("no").setParseAction(replaceWith(False)) quotedString.setParseAction(removeQuotes) unquotedString = Word(alphanums+"_-?\"") comment = Suppress("#") + Suppress(restOfLine) EQ,LBRACE,RBRACE = map(Suppress, "={}") data = (real | integer | yes | no | quotedString | unquotedString) #define structures value = Forward() object = Forward() dataList = Group(OneOrMore(data)) simpleArray = (LBRACE + dataList + RBRACE) propertyName = Word(alphanums+"_-.").setParseAction(self.prependPropertyToken) property = dictOf(propertyName + EQ, value) properties = Dict(property) object << (LBRACE + properties + RBRACE) value << (data | object | simpleArray) dataset = properties.ignore(comment) #parse it result = dataset.parseString(inputText) #turn it into a JSON-like object dict = self.convert_to_dict(result.asList()) return json.dumps(dict) def convert_to_dict(self, inputList): dict = {} for item in inputList: #determine the key and value to be inserted into the dict dictval = None key = None if isinstance(item, list): try: key = item[0].replace("__property__","") if isinstance(item[1], list): try: if item[1][0].startswith("__property__"): dictval = self.convert_to_dict(item) else: dictval = item[1] except AttributeError: dictval = item[1] else: dictval = item[1] except IndexError: dictval = None #determine whether to insert the value into the key or to merge the value with existing values at this key if key: if key in dict: if isinstance(dict[key], list): dict[key].append(dictval) else: old = dict[key] new = [old] new.append(dictval) dict[key] = new else: dict[key] = dictval return dict def prependPropertyToken(self,t): return "__property__" + t[0]