3

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'>
user9074332
  • 2,336
  • 2
  • 23
  • 39

2 Answers2

2

Your data is a string. You can convert it to a list with literal_eval:

import ast
data_list = ast.literal_eval(data)
len(data_list)
#2
DYZ
  • 55,249
  • 10
  • 64
  • 93
1

EDIT: I didn't saw earlier that DYZ answered before me but I would like to explain my answer a bit more.

There is a module called 'ast' that have the function 'literal_eval', as the name suggest, it evaluates the information of the txt as python code and also validates the input.

import os, ast

with open('/tmp/tmp.txt', encoding='utf-8') as data_file:
    data = ast.literal_eval(data_file.read())

print(len(data))
print(type(data))

Output:

2
<class 'list'>
Shai Léger
  • 845
  • 1
  • 8
  • 16