像使用python模块一样使用json文件

json-sempai的Python项目详细描述


https://travis-ci.org/kragniz/json-sempai.png?branch=masterhttps://pypip.in/version/json-sempai/badge.png?style=flat

你有没有在夜里睡不着觉,绝望地渴望 只需直接导入json文件,就好像它们是python模块一样 [1]?现在你可以了!

这种可憎让你可以写作

importsome_json_file

如果可以找到some_json_file.json,它就可以像 python模块。

用法

在python路径上的某个地方插入一个json文件。tester.json

{"hello":"world","this":{"can":{"be":"nested"}}}

现在导入jsonsempai和json文件

>>>fromjsonsempaiimportmagic>>>importtester>>>tester<module'tester'from'tester.json'>>>>tester.hellou'world'>>>tester.this.can.beu'nested'>>>

或者,可以使用上下文管理器(100%减少魔力):

>>>importjsonsempai>>>withjsonsempai.imports():...importtester>>>tester<module'tester'from'tester.json'>

还支持python包:

$ tree
.
└── python_package
    ├── file.json
    ├── __init__.py
    └── nested_package
        ├── __init__.py
        └── second.json
>>>fromjsonsempaiimportmagic>>>frompython_packageimportfile>>>file<module'python_package.file'from'python_package/file.json'>>>>importpython_package.nested_package.second>>>python_package.nested_package.second<module'python_package.nested_package.second'from'python_package/nested_package/second.json'>

安装

从pip安装:

$ pip install json-sempai

或者克隆此repo并从源安装:

$ python setup.py install

从您的机器中清除此恐怖:

$ pip uninstall json-sempai
[1]Disclaimer: Only do this if you hate yourself and the rest of the world.

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

推荐PyPI第三方库


热门话题
java Android首选项相同getPreference()值的不同数据   字符串如何在Java中连接两个列表中的单词   安卓 Java Socket编程:检测客户端和服务器是否连接   使用JTextField的java无法将值转换为int   在java中,如何在不通过超级构造函数设置的情况下将消息设置为自定义异常类   用于标识属性值的java正则表达式模式   Android中的java不可见谷歌地图   java正确取消启动ExecutorService的JavaFX任务   在非活动java类中使用安卓的融合位置提供程序,并在主活动类中获取经度和纬度   spring为什么我的大摇大摆不能用springboot在java中工作?   java JSF(2.2)ViewScope在Weblogic 12.2.1.2和JDK 8上使用Spring 4.3.7(在Mac OS和Docker Oracle Linux环境上)   java如何用一个按钮提高计时器速度   java如何检查字符是否是元音?   注册表引用了不存在的Java运行时环境安装或运行时已损坏错误   来自ResultSet java的mysql getDateTime   maven LanguageTool Java API是否具有“无用”依赖关系?   twitter api身份验证的java Trycatch问题   java在Apache Struts 1.1中是否可以显式白名单?   安卓致命异常:主java。lang.RuntimeException:被问及未知片段