#1. Reading using Pickle
import pickle
try:
geeky_file = open('GFG.txt', 'r')
dictionary_list = pickle.load(geeky_file)
for d in dictionary_list:
print(d)
geeky_file.close()
except:
print("Something unexpected occurred!")
#2. Reading from Text File:
def parse(d):
dictionary = dict()
# Removes curly braces and splits the pairs into a list
pairs = d.strip('{}').split(', ')
for i in pairs:
pair = i.split(': ')
# Other symbols from the key-value pair should be stripped.
dictionary[pair[0].strip('''""')] = pair[1].strip('''""')
return dictionary
try:
geeky_file = open('geeky_file.txt', 'rt')
lines = geeky_file.read().split('
')
for l in lines:
if l != '':
dictionary = parse(l)
print(dictionary)
geeky_file.close()
except:
print("Something unexpected occurred!")