0

I have a large text file i.e. pre.txt and I have want to read feat_left points over multiple lines using python. Here is the snapshot of the text file.

{
  "outputs": {
    "/home/113267806.jpg": {
      "feat_left": [
        2.369331121444702, 
        -1.1544183492660522
      ], 
      "feat_right": [
        2.2432730197906494, 
        -0.896904468536377
      ]
    }, 
    "/home/115061965.jpg": {
      "feat_left": [
        1.8996189832687378, 
        -1.3713303804397583
      ], 
      "feat_right": [
        1.908974051475525, 
        -1.4422794580459595
      ]
    }, 
    "/home/119306609.jpg": {
      "feat_left": [
        -0.7765399217605591, 
        -1.690917730331421
      ], 
      "feat_right": [
        -1.1964678764343262, 
        -1.9359161853790283
      ]
    }, 
cpwah
  • 141
  • 1
  • 3
  • 13

1 Answers1

3

Read the json and iterate like this,

import json
data = json.load(open('file_name.json'))
for val in data['outputs'].values():
     print(val['feat_left'])

Result will be like this,

[-0.7765399217605591, -1.690917730331421]
[2.369331121444702, -1.1544183492660522]
[1.8996189832687378, -1.3713303804397583]

Edit You can use like this for image path,

for key,val in a['outputs'].items():
         print(key,val['feat_left'])

And it will give liek this

('/home/119306609.jpg', [-0.7765399217605591, -1.690917730331421])
('/home/113267806.jpg', [2.369331121444702, -1.1544183492660522])
('/home/115061965.jpg', [1.8996189832687378, -1.3713303804397583])
Rahul K P
  • 15,740
  • 4
  • 35
  • 52