Exercise 115

1) - Write a python program which creates a file named data.txt and write the lines in this file: 

Name: David
Email: david@gmail.com
Phone: 33354587 age: 27 

2) - Write a Python program that reads the data.txt file and exports its content to a dictionary:

d = {'Name': 'David', 'Email': 'david@gmail.com', 'Phone': 33354587, age ': 27}


# Question 1
import os
# we open file in write mod
file = open("data.txt" , 'w')
#writing data into the file
file.write('Name: DavidnEmail: david@gmail.comnPhone: 33354587nage: 27n')
# Question 2
# create an empty dictionary that will contains the data file
d = dict({})

# now we can import the data from the data.txt file and export it into the dictionary
# open data.txt file in read mod
file = open("data.txt" , 'r')

# obtaining all content lines of data.txt file
listLines= file.readlines()
for line in listLines:
d[line.split(':')[0]] = line.split(':')[1]

Younes Derfoufi

Leave a Reply