Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

read list of dictionaries from file python

#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!")
    

Comment

PREVIOUS NEXT
Code Example
Python :: python loop 3 times 
Python :: How to assign value to variable in Python 
Python :: how to reduce the image files size in python 
Python :: return programming 
Python :: infinite for loop in python 
Python :: calculate iqr in python dataset example 
Python :: armstrong number function 
Python :: python script to sort file content 
Python :: numpy find index of matching values 
Python :: excelwriter python 
Python :: Examples using matplotlib.pyplot.quiver 
Python :: install python cap 
Python :: how to create Varible in python 
Python :: na in python 
Python :: python 3.6 release date 
Python :: pythom Lambda 
Python :: how to remove .0 from string column with empty strings in python 
Python :: somalia embassy in bangladesh 
Python :: x = 10 x += 12 y = x/4 x = x + y in python 
Python :: python seq 
Python :: como agregar elementos a un array en python 
Python :: loading .dat file in python 
Python :: iniciar un projecto de python con pyenv 
Python :: python prime number 
Python :: bolumden kalan python 
Shell :: bitnami restart apache 
Shell :: uninstall skype from ubuntu 
Shell :: ubuntu install telegram 
Shell :: maven test class 
Shell :: how to update git submodule 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =