Apr-08-2020, 03:51 PM
(This post was last modified: Apr-08-2020, 03:51 PM by cherry_cherry.)
Hi everyone,
My project is to enter a json file or txt file. If it exists in my source then read the file entered and evaluate the condition.
This condition: Create a new dictionary from the list in the selected file with the condition if value 'numero' duplicated so return new dictionnary emty, otherwise create a new dictionary.
But my difficulty now is when I enter the file name to check, if true, instead of announcing the file content, I have to enter the file name again before announcing the next content. And if the 'numero' value is duplicated, a new dictionary is created.
This's my code and the information output.
My project is to enter a json file or txt file. If it exists in my source then read the file entered and evaluate the condition.
This condition: Create a new dictionary from the list in the selected file with the condition if value 'numero' duplicated so return new dictionnary emty, otherwise create a new dictionary.
But my difficulty now is when I enter the file name to check, if true, instead of announcing the file content, I have to enter the file name again before announcing the next content. And if the 'numero' value is duplicated, a new dictionary is created.
This's my code and the information output.
import os import json #check file name in my source def saisie_nomf(lst): print("Donner le nom du fichier avec l'extension ['.json','.txt']: ") correct = False file = input() while correct == False: if("." not in file): print("Le manque . de l'extension!") print("Donner le nom de fichier valide: ") file = input() else: if os.path.splitext(file)[1] in lst: if file not in l: print("Fichier introuvable!") print("Donner le nom de fichier valide: " ) file = input() else: correct = True else: print("Extension incorrecte!") print("Donner le nom de fichier valide: ") file = input() return file #check value 'numero': duplicated or not def check(seq): for i in seq: (key, value), *rest = i.items() if value not in new_dict: new_dict[value] = dict(rest) else: return("Numero duplicated! " + "\n---->" + value + "\nFichier non utilisable!") return new_dict path = os.getcwd() l = os.listdir(path) new_dict = {} print("Voici le contenu de dossier " + path + " - Recherche: \n" ) for file in l: print("- " + file + "\n") list=['.json','.txt'] print("Voici le fichier choisi: " +saisie_nomf(list)) with open(saisie_nomf(list), 'r', encoding="utf-8") as liste: f = json.load(liste) check(f) print("Données source: ") print(f)
Output:Voici le contenu de dossier /Users/adelineng/PycharmProjects/TD5 - Recherche:
- TD5_exo3.py
- source1.json
- TD5_exo2.py
- source1bad3.json
- test.py
- source1bad2.json
- source1.txt
- source1bad1.json
- TD5_exo1.py
- .idea
- TD5_exo4.py
Donner le nom du fichier avec l'extension ['.json','.txt']:
souce1b
Le manque . de l'extension!
Donner le nom de fichier valide:
source1bad1.json
Voici le fichier choisi: source1bad1.json #correct
Donner le nom du fichier avec l'extension ['.json','.txt']: #I need enter one more time.
source1bad1.json
Données source:
[{'numero': '21212121', 'name': 'Dupond', 'first': 'Alain', 'notes': [11, 9.5, 5.5, 18]}, {'numero': '21202120', 'name': 'Bru', 'first': 'Mélissa', 'notes': [11, 19.5, 15, 8]}, {'numero': '20212023', 'name': 'Bosse', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}, {'numero': '20212023', 'name': 'Bosse', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}]
#value 'numero' duplicated 20212023 but it's not return a dict emty and the information numero duplicated like I want in my code.
Dictionnaire extrait:
21212121 - Dupond , Alain , [11, 9.5, 5.5, 18]
21202120 - Bru , Mélissa , [11, 19.5, 15, 8]
20212023 - Bosse , Mélissa , [13, 19.5, 15, 8]
None
Vous souhaitez trier selon :
1 - Tri Numero ASC
2 - Tri Numero DESC
3 - Tri Nom
4 - Tri Prénom
5 - Tri Notes ASC
6 - Tri Notes DESC
7 - Tri Moy DESC
8 - Quitter
Donner votre choix [1 - 8] :