Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print format
#1
Hi Gigs,

Below is a code that I'm working on:
def class_tuple_create(filename):
    try:
        file = open (filename, 'r')
    except:
        print(filename, 'Cannot be opened')
    else:
        myDict = {}
        for line in file:
            list = line.split()
            course_id = list[0]
            room = list[1]
            name = list[2]
            time = list[3]
            class_data = (room, name, time)
            myDict[course_id] = class_data
        file.close()
        return myDict

def print_classes(myDict):
    print ('ID\tRoom\tInstr\tStart')
    print ('------------------------------')
    for key in sorted(myDict):
        print (key, myDict[key])


class_information = class_tuple_create('xxxxx')
class_information = class_tuple_create('courses.txt')
print_classes(class_information)
I was expecting the output below
Output:
Error: Unable to open xxxxx ID Room Instr Start ----------------------------------- CM241 1411 Lee 13:00 CS101 3004 Haynes 8:00 CS102 4501 Smith 9:00 CS103 6755 Rich 10:00 NT110 1244 Burke 11:00
but it's printing the following:
Output:
Error: Unable to open xxxxx !!! ID      Room    Instr   Start ------------------------------ CM241 ('1411', 'Lee', '13:00') CS101 ('3004', 'Haynes', '8:00') CS102 ('4501', 'Smith', '9:00') CS103 ('6755', 'Rich', '10:00') NT110 ('1244', 'Burke', '11:00')
Please any advise on how to remove the quotes by printing individual element ?
Below is the ouput of the courses.txt file
Output:
CS101 3004 Haynes 8:00 CS102 4501 Smith 9:00 CS103 6755 Rich 10:00 NT110 1244 Burke 11:00 CM241 1411 Lee 13:00
Reply
#2
Input sample please
Reply
#3
When you use:
print (key, myDict[key])
Python just takes the standard string representation of the values (and since you value are tuples, you get a tuple display). If you want more control, you have ti unpack the tuples, and then print each part individually:
room,name,time= myDict[key]
print (key,room,name,time)
In practice, you should use a string format() for such output.

Otherwise:
  • myDict is a truly awful name for a variable. Why not courses?
  • In the same vein, the key in for key in sorted(myDict) would be better named course_id
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print first day of the week as string in date format MyerzzD 2 2,011 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  Fetch Oracle DB rows & print it in HTML file with table's col headers in table format tssr_2001 1 2,982 Sep-04-2020, 01:39 PM
Last Post: ibreeden
  misunderstanding of format in print function Harvey 2 2,175 Oct-29-2019, 12:44 PM
Last Post: buran
  problem with print format anna 7 4,344 May-16-2018, 11:28 AM
Last Post: anna

Forum Jump:

User Panel Messages

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