ui_io.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:ui2 作者: controversial 项目源码 文件源码
def _json_get(inp):
    """ Get a Python object (list or dict) regardless of whether data is passed
    as a JSON string, a file path, or is already a python object.

    Returns the parsed data, as well as "native" if the data was already a
    Python object, "str" if the data was passed as a JSON string, or the path
    if the data passed was a file path """

    if not (isinstance(inp, dict) or isinstance(inp, list)):  # Python object
        try:                                                    # JSON string
            data = json.loads(inp)
            dataformat = "str"
        except json.JSONDecodeError:                            # JSON filepath
            # Store the filename in the dataformat variable if dataformat is a
            # file,  because it's just one fewer variable to keep track of
            dataformat = inp
            with open(inp, encoding="utf-8") as f:
                data = json.load(f)
    else:
        dataformat = "native"

    return data, dataformat
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号