用于处理深度嵌套文档(列表和dict)的python函数

nested-lookup的Python项目详细描述


https://img.shields.io/badge/pypi-0.2.18-green.svghttps://travis-ci.org/rameshrvr/nested-lookup.svg?branch=master

再次让使用json、yaml和xml文档响应变得有趣!

嵌套查找包提供许多python函数,用于处理深度嵌套的文档。 在本例中,文档是python字典和列表对象(通常从yaml或json派生)的混合体。

嵌套查找:
对深度嵌套的文档执行密钥查找。 返回匹配值的列表。
嵌套更新:
给定一个文档,查找给定密钥的所有匹配项并更新该值。 默认情况下,返回文档的副本。 若要更改原始值,请指定in_place=true参数。
嵌套删除:
给定文档,查找给定密钥的所有匹配项并将其删除。 默认情况下,返回文档的副本。 若要更改原始值,请指定in_place=true参数。
嵌套改变:
给定一个文档,查找给定键的所有匹配项,并使用回调函数对其进行更改 默认情况下,返回文档的副本。 若要更改原始值,请指定in_place=true参数。
获取所有密钥:
从深度嵌套的字典中获取所有键。 返回键的列表。
get_occurrence_of_key/get_occurrence_of_value:
返回嵌套字典中键/值的出现次数。

例如函数调用,请参阅教程。

install

使用pip从pypi安装:

pip install nested-lookup

或简易安装:

easy_install nested-lookup

或使用以下命令从源安装:

git clone https://github.com/russellballestrini/nested-lookup.git
cd nested-lookup
pip install .

quick tutorial

>>>fromnested_lookupimportnested_lookup>>>document=[{'taco':42},{'salsa':[{'burrito':{'taco':69}}]}]>>>print(nested_lookup('taco',document))[42,69]>>>fromnested_lookupimportnested_update,nested_delete>>>nested_update(document,key='burrito',value='Test')[{'taco':42},{'salsa':[{'burrito':'Test'}]}]>>>nested_delete(document,'taco')[{},{'salsa':[{'burrito':{}}]}]

嵌套alter: 编写处理标量值的回调函数。 注意可以传递给回调函数的可能类型。 在本例中,我们可以确保只传递int,在生产环境中,您应该检查类型,因为它可以是任何类型。

>>>defcallback(data):>>>returndata+10# add 10 to every taco prize

alter版本只适用于标量输入(一个dict),如果需要寻址一个dict列表,则必须 手动迭代并将它们逐个传递给嵌套的更新

>>>out=[]>>>forelemindocument:>>>altered_document=nested_alter(elem,"taco",callback)>>>out.append(altered_document)>>>print(out)[{'taco':52},{'salsa':[{'burrito':{'taco':79}}]}]>>>fromnested_lookupimportget_all_keys>>>get_all_keys(document)['taco','salsa','burrito','taco']>>>fromnested_lookupimportget_occurrence_of_key,get_occurrence_of_value>>>get_occurrence_of_key(document,key='taco')2>>>get_occurrence_of_value(document,value='42')1

longer tutorial

您可以通过传递一些可选参数来控制函数的行为。

野生(默认为false):
如果wildtrue,则将给定的键视为不区分大小写 执行查找时的子字符串。
使用u键(默认为false):
如果带有u键的真,则返回所有匹配键的字典 以及一个值列表。

例如,给定以下文档:

fromnested_lookupimportnested_lookupmy_document={'name':'Russell Ballestrini','email_address':'test1@example.com','other':{'secondary_email':'test2@example.com','EMAIL_RECOVERY':'test3@example.com','email_address':'test4@example.com',},},

接下来,我们可以执行wild操作,并查找所有电子邮件地址,如下所示:

results=nested_lookup(key='mail',document=my_document,wild=True)print(results)
['test1@example.com','test4@example.com','test2@example.com','test3@example.com']

此外,如果您还需要匹配的密钥名称,可以这样做:

results=nested_lookup(key='mail',document=my_document,wild=True,with_keys=True,)print(results)
{'email_address':['test1@example.com','test4@example.com'],'secondary_email':['test2@example.com'],'EMAIL_RECOVERY':['test3@example.com']}

在嵌套文档中获取/删除/更新键->;值对

fromnested_lookupimportnested_update,nested_deleteresult=nested_delete(my_document,'EMAIL_RECOVERY')print(result)# result => {'other': {'secondary_email': 'test2@example.com', 'email_address': 'test4@example.com'}, 'email_address': 'test1@example.com', 'name': 'Russell Ballestrini'}result=nested_update(my_document,key='other',value='Test')print(result)# result => {'other': 'Test', 'email_address': 'test1@example.com', 'name': 'Russell Ballestrini'}

要获取文档中每个嵌套键的列表,请运行以下命令:

fromnested_lookupimportget_all_keyskeys=get_all_keys(my_document)print(keys)
['name','email_address','other','secondary_email','EMAIL_RECOVERY','email_address']

获取给定键/值的出现次数

fromnested_lookupimportget_occurrence_of_key,get_occurrence_of_valueno_of_key_occurrence=get_occurrence_of_key(my_document,key='email_address')print(no_of_key_occurrence)# result => 2no_of_value_occurrence=get_occurrence_of_value(my_document,value='test2@example.com')print(no_of_value_occurrence)# result => 1

misc

license:
  • Public Domain
authors:
  • Russell Ballestrini
  • Douglas Miranda
  • Ramesh RV
  • Salfiii (Florian S.)
web:

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
使用jaxb2annotateplugin和XJC工具的java自定义注释   java组织。xeustechnologies。jcl无法加载WstxInputFactory类   java JUnit在格式化字符串上比较失败   java Bukkit配置部分getKeys   如何关闭Java流?   java Struts2正则表达式配置   链式事务注释的java奇怪行为   java在两个JButton之间使用变量   java签署APK时内容会发生什么变化?   java LWJGL:Slick:3D世界中的绘图字体   如何分解Java数组?   在Java MySql中处理多个过滤器   java如何在Firebase数据库中跳过初始OnChildaded事件触发   java如何在PreviewView中使用CameraX?   在子类#中重写父类后访问父类原始方法的java已解决   java找不到类型的属性   游戏框架游戏!框架+Java