python - Sorting an imported list not working -
i trying sort imported list display have tried sorts of things , didnt work.
here exemple of list:
pommes : 54 bananes : 18 oranges : 30 ananas :12 clémentines :77 cerises de terre: 43
the output should ordered alphabeticaly
this getting
['\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', '1', '1', '2', '3', '3', '4', '4', '5', '7', '7', '8', ':', ':', ':', ':', ':', ':', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'c', 'c', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e' , 'e', 'e', 'g', 'i', 'i', 'l', 'm', 'm', 'm', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'o', 'o', 'p', 'r', 'r', 'r', 'r', 's', 's', 's', 's', 's', 's', 's', 't', 't ', 'é']
here's code
import sys def liste(entree): try: ouvrir = open("data2.txt") except ioerror: message1 = "le fichier data2 n'existe pas." return message1 lecture = ouvrir.read() if len(entree) < 1: message2 = "il faut préciser le nom du fichier à traiter" return message2 elif len(entree) > 1: message3 = "un seul argument est attendu, soit le nom du fichier à traiter." return message3 else: return lecture def main(): while true: entree = sys.argv[1:] choix = str(entree) texte = "data2.txt" if texte in choix: message4 = liste(entree) message4 = sorted(message4) print(message4) break else: print("il faut préciser le nom du fichier à traiter") exit() if __name__ == "__main__": main()
try readlines, (see answer: how read file line-by-line list?, see: reading text file , splitting single words in python).
oh, , 'with open()' idiomatic (moreso try),
with open("data2.txt") ouvrir: lines = ouvrir.readlines() print sorted(lines)
assuming each line contains single word, done.
suppose want treat each line words (one or more words per line), sort words on each line, , sort lines,
#open file "data2.txt" , readlines list #split each line words , sort list of sorted lines #words = list ( list ( word ) ) open("data2.txt") ouvrir: lines = ouvrir.readlines() line_words = [ x x in [ line.split(":") line in lines ] ] #line_names = [ x[0] x in [ line.split(":") line in lines ] ] print sorted(line_words)
suppose each line has 1 or more words, , want sorted list of words? following flattens nested list of words single list of words,
#open file "data2.txt" , readlines list #split each line words, flatten single list of words #words = list ( word ) open("data2.txt") ouvrir: lecture = ouvrir.readlines() words = [ word line in lecture word in line.split() ] print sorted(words)
suppose lines have key:value pairs, e.g. "apple: 23"?, want different
your program combines examining entree (slice sys.argv[1:]) opening , reading file. should separate 2 functions. here 1 way revise code,
import sys def liste(texte,entree): if len(entree) < 1: return "il faut préciser le nom du fichier à traiter" elif len(entree) > 1: return "un seul argument est attendu, soit le nom du fichier à traiter." open(texte) ouvrir: lecture = ouvrir.readlines() words = [ x.split(":")[0].strip() x in [ line.strip() line in lecture ] ] words = [ x x in words if len(x) > 1 ] #filter, remove short words (blanks) return lecture return "le fichier {} n'existe pas.".format(texte) def main(): while true: entree = sys.argv[1:] choix = str(entree) texte = "data2.txt" if texte in choix: message4 = sorted(liste(texte,entree)) print(message4) el in message4: print(el) break else: print("il faut préciser le nom du fichier à traiter") break if __name__ == "__main__": main() exit()
Comments
Post a Comment