简体   繁体   中英

List of Dictionary from .txt file using python

I have a text file with text formatted similar to the Dictionary format of python. I want to convert this text to list of Dictionary using Python. Please help. I am pasting the same text below. If anyone can help. Please mention the python code for converion.

{"x": 52.86634363112429, "y": 14.67862889415645, "width": 0.2638522427440633, "keypointlabels": ["left_eye"], "original_width": 433, "original_height": 217},
{"x": 56.91361729719117, "y": 15.412560338864274, "width": 0.2638522427440633, "keypointlabels": ["right_eye"], "original_width": 433, "original_height": 217},
{"x": 54.92677386112197, "y": 16.880423228279916, "width": 0.2638522427440633, "keypointlabels": ["nose"], "original_width": 433, "original_height": 217},
{"x": 53.52862477648069, "y": 20.256507873935902, "width": 0.2638522427440633, "keypointlabels": ["mouth_left_corner"], "original_width": 433, "original_height": 217},
{"x": 56.0305757700493, "y": 20.256507873935902, "width": 0.2638522427440633, "keypointlabels": ["mouth_right_corner"], "original_width": 433, "original_height": 217},
{"x": 54.70601347933652, "y": 23.045447363825627, "width": 0.2638522427440633, "keypointlabels": ["chin"], "original_width": 433, "original_height": 217},
{"x": 25.86605080831409, "y": 15.2073732718894, "width": 0.4618937644341801, "keypointlabels": ["left_eye"], "original_width": 433, "original_height": 217},
{"x": 29.099307159353348, "y": 13.824884792626728, "width": 0.4618937644341801, "keypointlabels": ["right_eye"], "original_width": 433, "original_height": 217}

Any time you're dealing with data formatted like python objects, you could use the ast package to evaluate it. In this case, you want ast.literal_eval

import ast

with open('path/to/file.txt') as f:
    result = []
    for line in f:
        result.append(ast.literal_eval(line.strip()))

This looks like a list in JSON format without the preceding and trailing brackets. You could perhaps parse it by adding those in:

import json

def parse_object_list(value_str):
    return json.loads("[" + value_str + "]")

Or if it's definitely a Python thing, you could use the ast.literal_eval function , which should safely parse a Python literal:

import ast

def parse_object_list(value_str):
    return ast.literal_eval("(" + value_str + ")")

In this example I have placed the value in parentheses so that python will read correctly-formed values as-is, and lines of comma-separated data like this as a tuple.

This code may work based on your input.

import json
filename="jsonfile.txt"
with open(filename) as file:
    lines = file.read()
list_of_dict=json.loads('['+lines+']')

#an empty dictionary

dictionary = {}
with open("abc.txt") as file: 
  for line in file: 
    (key, value) = line.split()
    dictionary[int(key)] = value 
print ('\ntext file to dictionary=\n',dictionary)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM