Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 将多个变量传递给函数_Python_Json - Fatal编程技术网

Python 将多个变量传递给函数

Python 将多个变量传递给函数,python,json,Python,Json,我正在进行一个项目,在这个项目中,我读取一个JSON文件,并检索与我正在处理的图像相关的不同信息 我一直在思考如何将多个变量传递给我的json read函数,并使用它们移动到我的json文件中,检索我需要的值(代码仅在传递3个变量时才起作用) 这是代码的一部分: class board(object): def __init__(self, json, image): self.json = json self.image = image def extract_

我正在进行一个项目,在这个项目中,我读取一个JSON文件,并检索与我正在处理的图像相关的不同信息

我一直在思考如何将多个变量传递给我的
json read
函数,并使用它们移动到我的json文件中,检索我需要的值(代码仅在传递3个变量时才起作用)

这是代码的一部分:

class board(object):
  def __init__(self, json, image):
    self.json = json
    self.image = image  


  def extract_json(one,two,three):
    with open('document.json') as data_file:
        data = json.load(data_file)
        return data[one][two][three]


  def crop_img(coords):
    im = Image.open('./tabellone.jpg')
    cropped_im = im.crop(coords)        
    return cropped_im

  score_home_coords = extract_json("boxes", "score_home", "coord")
  score_home_crop = crop_img(score_home_coords)
  score_home_crop.save("./images/score_home_cropped.jpg")
  path1 = "./images/score_home_cropped.jpg"
  command = "ssocr-2.16.3/ssocr -d -1 -b black -l red %s" % path1
  score_home = int(subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read())

MyAgonism = board('document.json', './tabellone.jpg')
print "Score Home: %s" % MyAgonism.score_home
我已经创建了一个MyAgonism类的实例,我将json文件名和我必须处理的图像文件名一起传递给它

init接收它们,但是如何在这两个类方法中使用它们呢?我想这样做:

def extract_json(one,two,three):
        with open(SELF.JSON) as data_file:
            data = json.load(data_file)
            return data[one][two][three]


def crop_img(coords):
        im = Image.open(SELF.IMAGE)
        cropped_im = im.crop(coords)        
        return cropped_im
JSON文件示例:

{
    "background": "black",
    "foreground": "red",
    "boxes": {
                "time_minutes": {"foreground": "red", "type" : "7segments" , "coord": [267,132,315,182]},
                "time_seconds": {"foreground": "red", "type" : "7segments" , "coord": [327,132,389,182]},
                "score_home" : {"foreground": "red", "type": "7segments", "coord": [204,51,276,101]},
...
提前感谢你的帮助


我需要这样的东西:

   def extract_json(self, *args): 
       with open('document.json') as data_file: 
          data = json.load(data_file) 
          return data[args] 
但所有参数都以这种方式显示:

(args1、args2、args3、args4)

而我需要的是

[args1][args2][args3][args4]

使用正确的键正确访问我的json文件


修正了,这个方法是无用的。我刚才用过:

with open(self.json_path) as f:
    self.d = json.load(f)

您刚刚忘记将
self
作为两个类方法的第一个参数:

def extract_json(self, one, two, three):
    with open(self.json) as data_file:
        ...

  def crop_img(self, coords):
    im = Image.open(self.image)
    ...
简化说明:

  • 类的方法,该类需要使用在 实例关联(实例特定值)必须将
    self
    作为第一个参数这些被称为 实例方法。
  • 不需要访问实例的类的方法 属性,但需要访问类属性或方法, 需要将
    cls
    作为第一个参数这些被称为类方法。
  • 不需要访问self且只执行 一些逻辑和返回值,不需要
    self
    ,也不需要
    cls
    这些是 调用静态方法。

更多解释

如果我将self添加为第一个参数,我会收到一个类型错误:
extract_json()正好接受4个参数(3个给定)
第一个问题有什么想法吗?好的,我已经解决了这个问题。现在它工作得很好,但在尝试调整extract_json函数以处理任何传递的参数时仍然存在一些问题(如下面的答案所示)