Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output File ?
#1
Hello I try to find the top 10 albums in the chinook database 18
Reply
#2
Don't print row[3], print something else. Is there a way to get a loop counter?
Reply
#3
i try to create a counter loop
Clearwater Revival tracks 18
Reply
#4
# Print the results
for n, (albumid, album, artist, tracks) in enumerate(result, start=1):
    print(f'{n}. {album} by {artist} - tracks: {tracks}')
Note, code not tested
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
I have found the solution

result = cur.fetchall()
count = 0

# Print the results
for row in result:
    count += 1
    print(count, row[1], 'by', row[2], '-', row[3])
Reply
#6
Last question eedence Clearwater Revival 18[/output]
Reply
#7
Formatting the print statement. While you are looking up how to format print commands in python you should also read about the enumerate command.
Reply
#8
Thanks i try the Formatting the print statement but can you please give a tip how i can do it
Reply
#9
(May-10-2023, 02:02 PM)Kessie1971 Wrote: Thanks i try the Formatting the print statement but can you please give a tip how i can do it
Google "python print formatting"?
Reply
#10
couldn't you just replace rows 26 and 27 from post 1 with:
for n, row in enumerate(result):
    print(f"row {n}: {row[n]}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reading text file and writing to an output file precedded by line numbers kannan 7 10,369 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Multiple XML file covert to CSV output file krish143 1 3,334 Jul-27-2018, 06:55 PM
Last Post: ichabod801
  How to get output in a file? BananaRekt 3 3,117 May-06-2018, 06:04 PM
Last Post: buran

Forum Jump:

User Panel Messages

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