[英]List of Dictionary from .txt file using python
I have a text file with text formatted similar to the Dictionary format of python.我有一个文本文件,其文本格式类似于 python 的字典格式。 I want to convert this text to list of Dictionary using Python.我想使用 Python 将此文本转换为字典列表。 Please help.请帮忙。 I am pasting the same text below.我在下面粘贴相同的文本。 If anyone can help.如果有人可以提供帮助。 Please mention the python code for converion.请提及 python 代码以进行转换。
{"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.任何时候您处理格式为 python 对象的数据时,都可以使用ast
package 来评估它。 In this case, you want ast.literal_eval
在这种情况下,您需要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.这看起来像 JSON 格式的列表,没有前后括号。 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:或者,如果它绝对是 Python 事物,您可以使用ast.literal_eval
function ,它应该安全地解析 ZA7F5F35426B927411FC9231B56382173 文字:
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.在此示例中,我将值放在括号中,以便 python 按原样读取格式正确的值,并将像这样的逗号分隔数据行作为元组读取。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.