CSV File Help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: CSV File Help (/thread-25934.html) |
CSV File Help - finndude - Apr-16-2020 Hi, I only want to display the first five rows and I also want to sort it from highest to lowest. not sure what to do so came here. print ("Time for the results!!!") #prints time for results time.sleep(1) #adds a one second break if amount_of_player_1_cards > amount_of_player_2_cards: #if the amount of cards player one has is bigger than the amount player two has print ("The winner, with " + amount_of_player_1_cards + " is....... " + username_1) #tells you the winner time.sleep(1) #adds a one seocnd break print (username_1 + " got the cards: ") #tells you what cards they got print (player_1_cards) #shows the cards time.sleep(1) #adds a one second break print ("That makes " + username_2 + " second place with " + amount_of_player_2_cards + " cards") #tells you who came 2nd with open("Card Game Scores.csv", "a+", newline = "") as save_score: #looking at what is in the file save_score.write(player_1_results) #writes in the file what the users results were save_score.write("\n") #adds a space to make it clearer save_score.close() #saves and closes the file else: print ("The winner, with " + amount_of_player_2_cards + " is....... " + username_2) #tells you the winner time.sleep(1) #adds a one second break print (username_2 + " got the cards: ") #tells you what cards they got print (player_2_cards) #shows the cards time.sleep(2) #adds a one second break print ("That makes " + username_1 + " second place with " + amount_of_player_1_cards + " cards") #tells you who came 2nd with open("Card Game Scores.csv", "a+", newline = "") as save_score: #looking at what is in the file save_score.write(player_2_results) #writes in the file what the users results were save_score.write("\n") #adds a space to make it clearer save_score.close() #saves and closes the file ##################################################################################################### time.sleep(2) #adds a 2 second break print ("This is how your scores compare to other winners of this game: ") #tells you this is how your socres compare to others time.sleep(1) #adds a 2 second break print ("This is these players most recent scores:") #tells you these are there most recent scores time.sleep(1) #adds a 1 second break all_scores = open("Card Game Scores.csv", "r") card_csv = csv.reader(all_scores,delimiter=",") sort_csv = sorted(card_csv,key=operator.itemgetter(1)) for eachline in sort_csv: print (eachline)this is the output: the list of values is all stored in a CSV fileI want the output to be highest to lowest and only show the top 5 scores. thanks. RE: CSV File Help - Larz60+ - Apr-16-2020 example you can modify: import csv def first_seven_rows(filename): with open(filename) as csvfile: reader = csv.reader(csvfile) for n, row in enumerate(reader): if n > 6: break print(f"{}n: {row}") RE: CSV File Help - finndude - Apr-16-2020 When I put this in and replace the filename with what it is I get a syntax error. what to do from here? RE: CSV File Help - Larz60+ - Apr-16-2020 always include exact error message in code tags. I didn't test code, but see an error on line 9 change: print(f"{}n: {row}")To: print(f"{n}: {row}") RE: CSV File Help - finndude - Apr-17-2020 With this in print ("Time for the results!!!") #prints time for results time.sleep(1) #adds a one second break if amount_of_player_1_cards > amount_of_player_2_cards: #if the amount of cards player one has is bigger than the amount player two has print ("The winner, with " + amount_of_player_1_cards + " is....... " + username_1) #tells you the winner time.sleep(1) #adds a one seocnd break print (username_1 + " got the cards: ") #tells you what cards they got print (player_1_cards) #shows the cards time.sleep(1) #adds a one second break print ("That makes " + username_2 + " second place with " + amount_of_player_2_cards + " cards") #tells you who came 2nd with open("Card Game Scores.csv", "a+", newline = "") as save_score: #looking at what is in the file save_score.write(player_1_results) #writes in the file what the users results were save_score.write("\n") #adds a space to make it clearer save_score.close() #saves and closes the file else: print ("The winner, with " + amount_of_player_2_cards + " is....... " + username_2) #tells you the winner time.sleep(1) #adds a one second break print (username_2 + " got the cards: ") #tells you what cards they got print (player_2_cards) #shows the cards time.sleep(2) #adds a one second break print ("That makes " + username_1 + " second place with " + amount_of_player_1_cards + " cards") #tells you who came 2nd with open("Card Game Scores.csv", "a+", newline = "") as save_score: #looking at what is in the file save_score.write(player_2_results) #writes in the file what the users results were save_score.write("\n") #adds a space to make it clearer save_score.close() #saves and closes the file #################################################################################################### time.sleep(2) #adds a 2 second break print ("This is how your scores compare to other winners of this game: ") #tells you this is how your socres compare to others time.sleep(1) #adds a 2 second break print ("This is these players most recent scores: ") #tells you these are there most recent scores time.sleep(1) #adds a 1 second break def first_seven_rows("Card Game Scores.csv"): with open(Card Game Scores.csv) as csvfile: reader = csv.reader(csvfile) for n, row in enumerate(reader): if n > 6: break print(f"{n}: {row}")I get the error the error is where I entered my filename I enter ("Card Game Scores.csv")the error was highlighting the apostrophe if I remove the apostrophes I get an invalid syntax highlighting the letter G on Card Game Scores. RE: CSV File Help - Larz60+ - Apr-17-2020 There must be more to the error message. Always include complete, unaltered, error traceback RE: CSV File Help - finndude - Apr-17-2020 The error is nothing more nothing lesshttps://imgur.com/a/E6Scghk Organising CSV File - finndude - Apr-18-2020 Hi, I only want to display the first five rows and I also want to sort it from highest to lowest. not sure what to do so came here. print ("Time for the results!!!") #prints time for results time.sleep(1) #adds a one second break if amount_of_player_1_cards > amount_of_player_2_cards: #if the amount of cards player one has is bigger than the amount player two has print ("The winner, with " + amount_of_player_1_cards + " is....... " + username_1) #tells you the winner time.sleep(1) #adds a one seocnd break print (username_1 + " got the cards: ") #tells you what cards they got print (player_1_cards) #shows the cards time.sleep(1) #adds a one second break print ("That makes " + username_2 + " second place with " + amount_of_player_2_cards + " cards") #tells you who came 2nd with open("Card Game Scores.csv", "a+", newline = "") as save_score: #looking at what is in the file save_score.write(player_1_results) #writes in the file what the users results were save_score.write("\n") #adds a space to make it clearer save_score.close() #saves and closes the file else: print ("The winner, with " + amount_of_player_2_cards + " is....... " + username_2) #tells you the winner time.sleep(1) #adds a one second break print (username_2 + " got the cards: ") #tells you what cards they got print (player_2_cards) #shows the cards time.sleep(2) #adds a one second break print ("That makes " + username_1 + " second place with " + amount_of_player_1_cards + " cards") #tells you who came 2nd with open("Card Game Scores.csv", "a+", newline = "") as save_score: #looking at what is in the file save_score.write(player_2_results) #writes in the file what the users results were save_score.write("\n") #adds a space to make it clearer save_score.close() #saves and closes the file ##################################################################################################### time.sleep(2) #adds a 2 second break print ("This is how your scores compare to other winners of this game: ") #tells you this is how your socres compare to others time.sleep(1) #adds a 2 second break print ("This is these players most recent scores:") #tells you these are there most recent scores time.sleep(1) #adds a 1 second break all_scores = open("Card Game Scores.csv", "r") card_csv = csv.reader(all_scores,delimiter=",") sort_csv = sorted(card_csv,key=operator.itemgetter(1)) for eachline in sort_csv: print (eachline)this is the output: the list of values is all stored in a CSV fileI want the output to be highest to lowest and only show the top 5 scores. thanks. RE: Organising CSV File - anbu23 - Apr-18-2020 all_scores = open("Card Game Scores.csv", "r") card_csv = csv.reader(all_scores,delimiter=",") sort_csv = sorted(card_csv,key=lambda elem: int(elem[1]),reverse=True) for count,eachline in enumerate(sort_csv): if count<5: print (eachline) |