'Give each table a row value according to it's x and y coordinates

I am trying to give an alphabetical letter to each table_row field in this dictionary according to the x axis, and a number to each table_number according to the y axis.

My JSON file looks like this:

{
  "table": [
    [
      {
        "table_row": "",
        "table_number": 0,
        "table_serial": "",
        "xmax": 640.0,
        "ymax": 418.1505432129,
        "xmin": 142.3304901123,
        "ymin": 93.9450378418
      },
      {
        "table_row": "",
        "table_number": 0,
        "table_serial": "",
        "xmax": 640.0,
        "ymax": 418.2640991211,
        "xmin": 156.3077545166,
        "ymin": 91.5001678467
      }]}

This is the code I've written so far, but I can't think of a good way to do it:

with open("orderddata.json", "r+") as m:
    js.dump(l, m, indent=2)
    list_row = string.ascii_uppercase
    for table in dict["table"]:
        temp = table["position"]["xmax"]
        for i in len(dict["table"]):
            if table["position"]["xmax"] >= temp :
                temp = table["position"]["xmax"]
                table["row"] = list_row[i:1]
    for table in dict["table"]:
        temp = table["position"]["ymax"]
        for i in len(dict["table"]):
            if table["position"]["ymax"] >= temp :
                temp = table["position"]["ymax"]
                table["table_number"] = i

The expected result is:

{
  "table": [
    [
      {
        "table_row": "A",
        "table_number":2 ,
        "table_serial": "",
        "xmax": 640.0,
        "ymax": 500.1505432129,
        "xmin": 142.3304901123,
        "ymin": 93.9450378418
      },
      {
        "table_row": "A",
        "table_number": 1,
        "table_serial": "",
        "xmax": 640.0,
        "ymax": 300.2640991211,
        "xmin": 156.3077545166,
        "ymin": 91.5001678467
      }]}


Solution 1:[1]

It is not clear what you are trying to do, but based on your code snippet and the (not well-formed) JSON sample, here is a possible solution:

import json
from string import ascii_uppercase

json_string = '''
{
  "table": [[
    {
      "table_row": "",
      "table_number": "",
      "table_serial": "",
      "xmax": 640.0,
      "ymax": 418.1505432129,
      "xmin": 142.3304901123,
      "ymin": 93.9450378418
    },
    {
      "table_row": "",
      "table_number": "",
      "table_serial": "",
      "xmax": 640.0,
      "ymax": 418.2640991211,
      "xmin": 156.3077545166,
      "ymin": 91.5001678467
    }
  ]]
}'''

tables = json.loads(json_string)

xmax = 0
ymax = 0

for i, table in enumerate(tables['table']):
    for j, row in enumerate(table):
        # find max(xmax)
        if row['xmax'] > xmax:
            xmax = row['xmax']

        # find max(ymax)
        if row['ymax'] > ymax:
            ymax = row['ymax']

        # fill row and table_number
        tables['table'][i][j]['table_row'] = ascii_uppercase[i]
        tables['table'][i][j]['table_number'] = j + 1

print('xmax:', xmax)
print('ymax:', ymax)
print(json.dumps(tables, indent=2))

The code above will output the following:

xmax: 640.0
ymax: 418.2640991211
{
  "table": [
    [
      {
        "table_row": "A",
        "table_number": 1,
        "table_serial": "",
        "xmax": 640.0,
        "ymax": 418.1505432129,
        "xmin": 142.3304901123,
        "ymin": 93.9450378418
      },
      {
        "table_row": "A",
        "table_number": 2,
        "table_serial": "",
        "xmax": 640.0,
        "ymax": 418.2640991211,
        "xmin": 156.3077545166,
        "ymin": 91.5001678467
      }
    ]
  ]
}

But bear in mind that the code above will fail if the table list has more rows than len(ascii_uppercase). You didn't tell us what to do in that case.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1