0
labels1 = [{'yMax': u'780.797833935018', 'xMax': u'587.5438596491229', 'xMin': u'366.84210526315786',
                'yMin': u'613.9097472924188', 'id': '1', 'name': 'start'},
               {'yMax': u'244.37184115523468', 'xMax': u'2096.6666666666665', 'xMin': u'2057.8947368421054',
                'yMin': u'125.16606498194946', 'id': '2', 'name': ''},
               {'yMax': u'146.02707581227438', 'xMax': u'533.859649122807', 'xMin': u'477.1929824561403',
                'yMin': u'119.2057761732852', 'id': u'3', 'name': ''},
               {'yMax': u'336.7563176895307', 'xMax': u'838.0701754385965', 'xMin': u'775.438596491228',
                'yMin': u'238.4115523465704', 'id': u'4', 'name': ''},
               {'yMax': u'235.43140794223828', 'xMax': u'214.73684210526315', 'xMin': u'187.89473684210526',
                'yMin': u'149.0072202166065', 'id': u'5', 'name': ''},
               {'yMax': u'420.20036101083036', 'xMax': u'456.3157894736842', 'xMin': u'426.49122807017545',
                'yMin': u'330.79602888086646', 'id': u'6', 'name': 'end'}

               ]

My data is a list of dictionaries as the example shared above just more dictionaries in the list. That example has them hardcoded but in the real situation they're in a txt file, the issue I have is making them work properly read from the txt file in that format.

The data as they look in my .txt file

{'yMax': u'156', 'xMax': u'4802', 'xMin': u'4770','yMin': u'141', 'id': '1', 'name': ''}
{'yMax': u'157', 'xMax': u'4895', 'xMin': u'4810','yMin': u'141', 'id': '2', 'name': ''}

The thing is further processing worked on the hardcoded version but didn't when I read them from a file, because i'm probably reading them as strings not as dictionaries and not sure what the most efficient way to fix that is, I tried reading them as json.load or line by line, then using re.findall(r'\d+', str(labels1)) to get the numbers and appending a dict 1 by 1 but it's a hasstle and didn't even work.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jess
  • 205
  • 2
  • 11

2 Answers2

1

Yes, when you read file, the content is saved to string variable. You can use eval function on this variable, to convert it to exactly what you want. Documentation. But be aware, that eval isn't secure, so consider also this solution.

In your concrete case it should look like:

with open("my_file_with_formated_data", "r") as file:
    my_data = file.read()
    labels1 = eval(my_data)

You could also use ast literal eval instead of eval to make sure everything is safe.

Qback
  • 4,310
  • 3
  • 25
  • 38
  • eval can be very dangerous, especially when working with data from untrusted sources. In the given case, I think [ast's literal_eval](https://docs.python.org/3/library/ast.html#ast.literal_eval) would be a better choice – shmee Apr 16 '18 at 09:07
  • @Qback thanks, but would a more specific edit to your answer on how to treat my exact case be possible. – Jess Apr 16 '18 at 09:23
1

you can use ast which is more safe than eval. read more: Convert a String representation of a Dictionary to a dictionary?

import ast

file = open("file.txt", 'r')

for line in file.readlines():
    dicObj = ast.literal_eval(line.strip('\n'))
    print(isinstance(dicObj, dict))

True

Mujtaba Aldebes
  • 123
  • 1
  • 7