Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将json文件传递给方法_Python_Json - Fatal编程技术网

在Python中将json文件传递给方法

在Python中将json文件传递给方法,python,json,Python,Json,我的计算机上有一个json文件,需要传递给以下函数: def read_json(file): try: logging.debug('Reading from input') return read_json_from_string(json.load(file)) finally: logging.debug('Done reading') 如何将文件从计算机移动到python中的方法?我尝试了以下方法: file = os.

我的计算机上有一个json文件,需要传递给以下函数:

def read_json(file):
    try:
        logging.debug('Reading from input')
        return read_json_from_string(json.load(file))
    finally:
        logging.debug('Done reading')
如何将文件从计算机移动到python中的方法?我尝试了以下方法:

file = os.path.abspath('theFile.json')
然后尝试按以下方式运行该方法:

read_json(file)
但我得到了以下错误:

TypeError: expected file
我还尝试:

file = open('theFile.json', 'r')

但我总是会遇到一个与“文件”不是文件相关的错误

json.load
获取一个类似文件的对象,并向其传递一个包含路径的
str
。请尝试以下方法:

path = os.path.abspath('theFile.json')
with open(path) as f:
    read_json(f)

请注意,
json.load
返回一个字典,而不是字符串。此外,即使在
try:
中引发异常,也会执行
finally:
,因此即使发生错误且读取被中止,您也会始终记录“完成读取”。

json.load
获取类似文件的对象,并向其传递包含路径的
str
。请尝试以下方法:

path = os.path.abspath('theFile.json')
with open(path) as f:
    read_json(f)
请注意,
json.load
返回一个字典,而不是字符串。此外,即使在
try:
中引发异常,也会执行
finally:
,因此即使发生错误并中止读取,您也会始终记录“已完成读取”。

=UPDATED=====

现在包括一个调用函数的示例


尝试以下方法:

import logging
import json

def read_json(file):
    try:
        print('Reading from input')
        with open(file, 'r') as f:
            return json.load(f)
    finally:
        print('Done reading')

return_dict = read_json("my_file.json")
print return_dict
======已更新=====

现在包括一个调用函数的示例


尝试以下方法:

import logging
import json

def read_json(file):
    try:
        print('Reading from input')
        with open(file, 'r') as f:
            return json.load(f)
    finally:
        print('Done reading')

return_dict = read_json("my_file.json")
print return_dict

尝试写入文件的完整路径(或相对于程序的路径),而不仅仅是名称。否则,你的程序怎么可能知道它的位置呢?试着写文件的完整路径(或相对于程序的路径),而不仅仅是名称。否则,你的程序怎么可能知道它的位置呢?现在我得到了错误值error:Expected object或value@Cybernetic我猜错误在
从字符串读取json中,但是如果没有更详细的回溯,很难知道问题是什么。现在我得到了错误值error:Expected object或value@Cybernetic我猜错误在
从字符串读取json中,但是,如果没有更详细的回溯,很难知道问题出在哪里。这一条告诉我:TypeError:强制使用Unicode:需要字符串或缓冲区,文件查找您可能正在传入一个文件对象。我已经更新了答案,包括调用函数的示例;希望这将有助于让事情为你工作。谢谢你的更新。更新现在给出:ValueError:无法解码JSON对象当您试图读取的文件实际上不包含JSON文本时发生的错误;
json.load()
函数找到了您的文件,但无法解析该文件,因为该文件不包含json或json格式错误。是否有可能包含您尝试解析的文件(或文件的一部分)?这将帮助我们找出文件无法正确解析的原因。下面给出:TypeError:强制使用Unicode:需要字符串或缓冲区,文件已找到您可能正在传递文件对象。我已经更新了答案,包括调用函数的示例;希望这将有助于让事情为你工作。谢谢你的更新。更新现在给出:ValueError:无法解码JSON对象当您试图读取的文件实际上不包含JSON文本时发生的错误;
json.load()
函数找到了您的文件,但无法解析该文件,因为该文件不包含json或json格式错误。是否有可能包含您尝试解析的文件(或文件的一部分)?这将帮助我们找出文件无法正确解析的原因。