1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Données qui pourraient être lue d'un fichier
f = open('contact.txt', 'w+')
NOM, TEL, EMAIL, DATE = (0, 1, 2, 3) # indices constants
liste = f.read().splitlines()
carnet = [] # Notre carnet de contact qui est une liste de listes
for entry in liste:
carnet.append(entry.split(';'))
def recherche_telephone(carnet, num_tel):
""" Retourne le contact avec le nom fourni """
for entry in carnet:
if entry[NOM] == nom_tel:
return entry
return None # C'est la valeur retournée par défaut par Python si on ne fait
# pas de return.
def affiche_contact(contact):
print("""\
Nom: {0}
Numéro de téléphone: {1}
Adresse email: {2}
Date de naissance: {3}""".format(contact[NOM], contact[TEL], contact[EMAIL], contact[DATE]))
nom_tel = input("Entrez un nom: ")
pseudo = input("pseudo")
def addColumn(lines, nameColumn, newColumn):
index = lines[0].index(nameColumn)
lines[0].insert(index, newColumn)
for line in lines[1:]:
line.insert(index, str(pseudo))
test = [
['Nom', 'Tel', 'email', 'date'],
['Bob', '0606060607', 'bob@contact.fr', '02/05/1997'],
]
addColumn(test, 'email', 'pseudo')
#print(test)
contact_trouve = recherche_telephone(carnet, nom_tel)
if contact_trouve is None:
print("Aucun contact trouvé avec le nom", nom_tel)
else:
print("Ce nom a pour information:")
affiche_contact(contact_trouve)
lines = []
for line in f:
LISTES = line.split(';')
lines.append(LISTES)
addColumn(lines, 'email', 'pseudo')
for line in lines:
phrase = ';'.join(line)
f.write(phrase + '\n')
f.close() |
Partager