I have an input text file containing a list of key/value pairs that I would like to read into python as a list of dictionaries but can not seem to get it to work as expected. Note that the file is not in valid json format so I can not use the json
built-in and this question is not a duplicate of this. I suspect that I am missing something obvious here so any guidance is much appreciated.
# /tmp/tmp.txt
[{'k1': {'k2': {'k3': ['a', 'b', 'c']}}}, {'k4': {'k5': {'k6': ['v', 'x', 'y', 'z']}}}]
Since this file contains a list with 2 elements, I would expect the len
to be 2 and the type
to be list
but that is not what I'm seeing.
with open('/tmp/tmp.txt', encoding='utf-8') as data_file:
data = data_file.read()
print(len(data)) # <-- the goal is for this to show 2
print(type(data)) # <-- the goal is for this to return `list`
Output:
88
<class 'str'>