Exercise 114
We consider the Pythons dictionary which contains a personal data :
personalData = {'Name' : 'David' , 'Email' : 'david@gmail.com' , 'Phone' : 33354587 , age' : 27 }
Write a Python program that save data contained in personalData into a file called data.txt .
Solution
import os
personalData = {'Name' : 'David' , 'Email' : 'david@gmail.com' , 'Phone' : 33354587 , 'age' : 27 }
# we open file in write mod
file = open("data.txt" , 'w')
for key , value in personalData.items() :
file.write(key + " : " + str(personalData[key]) + 'n')
file.close()
os.startfile("data.txt")
The output will display a txt file containing the dictionary data:
Younes Derfoufi
my-courses.net
my-courses.net