47_Read_file_contents_into_dictionary_strip_split_using_regex.py


Sign up Free. Don't forget to check out our challenges, lessons, solve and learn series and more ...


Code Snippet

"""File Contents
"['a', 5]"
"['b', 2]"
"['c', 3]"
"['d', 0]"
"""
import re 

def readfiletodict():
   
   with open("testfile.txt","r",newline="") as f:
     mydict={} #create a dictionary called mydict
     for line in f:
        lineFormat = re.sub('[^A-Za-z0-9,]+', '', line)
        (key,val) = lineFormat.split(",")
        mydict[key]=val
     print(mydict) #test
     for keys in mydict:
       print(keys) #test to see if the keys are being retrieved correctly
       

readfiletodict()    
                    

Try it yourself