我的计算机上有一个 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')
但我总是收到与"file"不是文件相关的错误。
最佳答案
=====已更新=====
现在包括一个调用函数的例子
尝试这样的事情:
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
关于python - 将 json 文件传递给 Python 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28121455/