This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| informatique:python [2024/11/18 08:50] – created yahiko | informatique:python [2024/12/13 21:11] (current) – yahiko | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Python ====== | ====== Python ====== | ||
| - | Je débute en Python, je jette ici mes notes en vrac. | + | Je débute en Python, je jette ici mes notes en vrac. Comme d' |
| ===== Initialiser un environnement virtuel (venv) ===== | ===== Initialiser un environnement virtuel (venv) ===== | ||
| Line 19: | Line 19: | ||
| <code python> | <code python> | ||
| nom_variable = " | nom_variable = " | ||
| + | </ | ||
| + | |||
| + | ===== Tableau ===== | ||
| + | (je crois) | ||
| + | |||
| + | <code python> | ||
| + | tableau = [ | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ] | ||
| </ | </ | ||
| Line 27: | Line 38: | ||
| f.close | f.close | ||
| </ | </ | ||
| + | |||
| + | En UTF-8 : | ||
| + | |||
| + | <code python> | ||
| + | import codecs | ||
| + | |||
| + | f = codecs.open(nomfichier.txt, | ||
| + | f.write(" | ||
| + | f.close | ||
| + | </ | ||
| + | |||
| + | Depuis un tableau : | ||
| + | <code python> | ||
| + | import codecs | ||
| + | |||
| + | f = codecs.open(nomfichier.txt, | ||
| + | for line in tableau: | ||
| + | f.write(line) | ||
| + | f.write(' | ||
| + | f.close | ||
| + | </ | ||
| + | |||
| ===== Date et heure ===== | ===== Date et heure ===== | ||
| <code python> | <code python> | ||
| import datetime | import datetime | ||
| + | |||
| # Récupère la date courante | # Récupère la date courante | ||
| date_now = datetime.datetime.now() | date_now = datetime.datetime.now() | ||
| Line 38: | Line 72: | ||
| date = str(date_now.strftime(" | date = str(date_now.strftime(" | ||
| </ | </ | ||
| + | |||
| + | ===== Vérifier si un dossier existe ===== | ||
| + | <code python> | ||
| + | import os | ||
| + | |||
| + | if os.path.isdir(/ | ||
| + | print(" | ||
| + | else: | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | ===== Trouver la longueur d'une chaîne de caractères ===== | ||
| + | <code python> | ||
| + | string = " | ||
| + | print(len(string)) | ||
| + | </ | ||
| + | Va retourner '' | ||
| + | |||
| + | ===== Le dernier caractère d'une chaîne ===== | ||
| + | <code python> | ||
| + | print(" | ||
| + | </ | ||
| + | Va retourner '' | ||
| + | |||
| + | ===== Fonctions ===== | ||
| + | ==== Définir une fonction ==== | ||
| + | <code python> | ||
| + | def fonction(): | ||
| + | blabla | ||
| + | </ | ||
| + | |||
| + | ==== Utilisation ==== | ||
| + | <code python> | ||
| + | fonction(quelquechose) | ||
| + | </ | ||
| + | |||
| + | ==== Exemples ==== | ||
| + | === Enlever des accents === | ||
| + | <code python> | ||
| + | import unidecode | ||
| + | |||
| + | def remove_accents(mot): | ||
| + | wAccents = unidecode.unidecode(wQuote) | ||
| + | return wAccents | ||
| + | |||
| + | mot = hémidécérébellé | ||
| + | mot_sansaccents = remove_accents(mot) | ||
| + | print mot_sansaccents | ||
| + | </ | ||
| + | |||
| + | === Raccourcir une chaîne === | ||
| + | <code python> | ||
| + | def shorten(short): | ||
| + | # On récupère le nombre de caractères de la chaîne en integer | ||
| + | nb = int(len(short)) | ||
| + | # Si supérieur à la variable qui définir la longueur on raccourci | ||
| + | if nb > lenght: | ||
| + | string = str(short[0: | ||
| + | # Sinon on affiche tout | ||
| + | else: | ||
| + | string = short | ||
| + | return string | ||
| + | # Longueur max et longueur retournée | ||
| + | lenght = 20 | ||
| + | chaine = " | ||
| + | |||
| + | print(shorten(chaine)) | ||
| + | </ | ||
| + | |||
| + | === Faire un truc si le dernier caractère d'une chaîne est quelque chose === | ||
| + | Je ne sais pas, un '' | ||
| + | <code python> | ||
| + | def DashCheck(check): | ||
| + | if check == ' | ||
| + | dash = str(" | ||
| + | else: | ||
| + | dash = str(" | ||
| + | return dash | ||
| + | |||
| + | print(DashCheck(" | ||
| + | </ | ||
| + | Va retourner '' | ||
| + | |||
| + | Et si on veut supprimer ce dernier '' | ||
| + | <code python> | ||
| + | def DashCheck(check): | ||
| + | # On vérifie le dernier caractère de la chaîne | ||
| + | if check[-1] == ' | ||
| + | # Si " | ||
| + | dash = check[:-1] | ||
| + | return dash | ||
| + | else: | ||
| + | return check | ||
| + | </ | ||
| + | |||