Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
-> Help: erreur print
#5
(Apr-08-2020, 07:51 PM)deanhystad Wrote: No. You need to call the function once to get the filename. Then you can use the returned filename as many times as you want.

filename = saisie_nomf(list))
print("Voici le fichier choisi: "+filename)

with open(filename, 'r', encoding="utf-8") as liste:

Thank you so much! I fixed. But how can I fix input like If the number is duplicated then the following contents are not notified
Now, I'm trying my code like this:

import os
import json

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

def check(seq):
    for i in range(len(seq) - 1):
        for j in range(i + 1, len(seq)):
            if seq[i]['numero'] == seq[j]['numero']:
                print("Numero en double!\n " + "----> " + seq[i]['numero'] + " \n Fichier non utilisable!")
                return new_dict  # Return unaltered dictionary

        for i in seq:
            (key, value), *rest = i.items()
            new_dict[value] = dict(rest)

    return new_dict

def affichageE(etud_dico):
    for elem,elem2 in etud_dico.items():
        print(elem,"-", elem2['name'],",",elem2['first'],",",elem2['notes'])

....

path = os.getcwd()
l = os.listdir(path)

print("Voici le contenu de dossier " + path + " - Recherche: \n" )
for file in l:
    print("- " + file + "\n")
list=['.json','.txt']

nom_saisie_fichier = saisie_nomf(list)
print("Voici le fichier choisi: " +nom_saisie_fichier)

with open(nom_saisie_fichier, 'r', encoding="utf-8") as liste:
    f = json.load(liste)

new_dict = {}
check(f)

print("\nDictionnaire extrait: ")
for k in new_dict: print(affichageE(k))

ListeChoix =["","Tri Numero ASC","Tri Numero DESC","Tri Nom ","Tri Prénom","Tri Notes ASC","Tri Notes DESC","Tri Moy DESC", "Quitter"]
ch = menu(ListeChoix)

while ch != len(ListeChoix)-1 :
    if ch == 1:
        print(" Tri selon les numéros des étudiants croissantes : ")
        for e in sorted(new_dict):  print(e,"-", new_dict[e]['name'],",",new_dict[e]['first'],",",new_dict[e]['notes'])
    elif ch == 2:
        print(" Tri selon les numéros des étudiants décroissantes : ")
        for e in sorted(new_dict, reverse=True): print(e,"-", new_dict[e]['name'],",",new_dict[e]['first'],",",new_dict[e]['notes'])
    elif ch == 3 :
        print("  Tri selon les noms:")
        for k, v in sorted(new_dict.items(), key=lambda x: x[1]['name']): print(k, "-", v['name'], ",", v['first'], ",", v['notes'])
    elif ch == 4 :
        print("  Tri selon les prénoms:")
        for k, v in sorted(new_dict.items(), key=lambda x: x[1]['first']): print(k, "-", v['name'], ",", v['first'], ",", v['notes'])
    elif ch == 5 :
        print("  Tri selon les notes croissantes :")
        for k, v in sorted(new_dict.items(), key=lambda x: x[1]['notes']): print(k, "-", v['name'], ",", v['first'], ",", v['notes'])
    elif ch == 6 :
        print("  Tri selon les notes décroissantes :")
        for k, v in sorted(new_dict.items(), key=lambda x: x[1]['notes'], reverse=True): print(k, "-", v['name'], ",", v['first'], ",", v['notes'])
    elif ch == 7 :
        print("  Tri selon la moyenne décroissante :")
        for k, v in sorted(new_dict.items(), key=lambda x: moyenne(x[1]['notes']), reverse=True):
            print(k, "-", v['name'], ",", v['first'], ",", v['notes'], "-", moyenne(v['notes']))
    ch = menu(ListeChoix)
Output:
Donner le nom du fichier avec l'extension ['.json','.txt']: source1bad1.json Voici le fichier choisi: source1bad1.json Numero en double! ----> 20212023 Fichier non utilisable! Dictionnaire extrait: Traceback (most recent call last): File "/Users/adelineng/PycharmProjects/TD5/TD5_exo4.py", line 95, in <module> for k in new_dict: print(affichageE(k)) File "/Users/adelineng/PycharmProjects/TD5/TD5_exo4.py", line 44, in affichageE for elem,elem2 in etud_dico.items(): AttributeError: 'str' object has no attribute 'items'
Reply


Messages In This Thread
-> Help: erreur print - by cherry_cherry - Apr-08-2020, 03:51 PM
RE: -> Help: erreur print - by deanhystad - Apr-08-2020, 05:19 PM
RE: -> Help: erreur print - by cherry_cherry - Apr-08-2020, 05:40 PM
RE: -> Help: erreur print - by deanhystad - Apr-08-2020, 07:51 PM
RE: -> Help: erreur print - by cherry_cherry - Apr-09-2020, 05:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Erreur de code python sur un template rpm de zabbix samba 0 2,026 Apr-02-2021, 09:43 AM
Last Post: samba
  Erreur Code access violation reading 0x00000004 yan_mhb 0 2,466 Jul-10-2020, 02:28 PM
Last Post: yan_mhb
  Erreur Code yan_mhb 2 2,252 Jul-09-2020, 03:50 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020