Read a dictionary from a text file in Python

2

I am trying to read a dictionary from a text file, this file has a structure like the following:

{'foo': 2, 'hola': 'Hello world!'}

I have researched and there are recommendations to use the JSON module but I would like to know if there is any other way, thank you very much.

    
asked by Luis Miguel 05.06.2018 в 22:00
source

1 answer

0

These are known as dictionaries

tabla = {'foo': 2, 'hola': 'Hello world!'}
for k,v in tabla.items():
  print(k,v)
'''
-------------------
se imprime:
-------------------
foo 2
hola Hello world!
'''

print(tabla.items())
# se imprime: dict_items([('foo', 2), ('hola', 'Hello world!')])

print(tabla.get('Saludos','No Encontrado'))
# se imprime: No Encontrado

tabla['Saludos'] = 'Que tal'
# se imprime: Que tal

This is something complementary to know how to manipulate the information that has been exposed in the initial question

    
answered by 05.06.2018 в 22:12