1

I have a list formatted like the following:

list_of_DVDsuppliers=[["a","m",15],["w","p",34]]

I'd like to print out the contents of this list as a table with some headers. Here's what I've tried so far:

def dvdprintsoftlist():
    print(list_of_DVDsoftwears)
    ''' printing the available DVDstocks,supplier's details '''
    print("This is your current list of stock")
    print("Supplier Name      Softwear Name             Amount")
    print("----------------------------------------------------------------")
    for (index,[name,softname,amount]) in enumerate (list_of_DVDsuppliers):
        print(name + "          " +softname + "            " +str(amount)  + "   ")
        print("----------------------------------------------------------------")
    print("")

The problem is that this code doesn't align the table columns properly. How can I make sure all the entries are aligned with each other?

I'd also like to export my data in CSV format so that other programs can read it, but that's a separate question.

3
  • i just use this code def dvdprintsoftlist(): print(list_of_DVDsoftwears) ''' printing the available DVDstocks,supplier's details ''' print("This is your current list of stock") print("Supplier Name Softwear Name Amount") print("----------------------------------------------------------------") for (index,[name,softname,amount]) in enumerate (list_of_DVDsuppliers): print(name + " " +softname + " " +str(amount) + " ") print("----------------------------------------------------------------") print("") Commented Oct 24, 2013 at 2:54
  • 1
    Have you found stackoverflow.com/questions/9535954/…? Commented Oct 24, 2013 at 3:24
  • its nt working its python 3 Commented Oct 24, 2013 at 3:44

2 Answers 2

2

You could use format(), the {i:length} in string, will be replaced by the parameters, where i is an order identifier, and length is the "length" of the field.

def dvdprintsoftlist():
    list_of_DVDsuppliers=[["a","m",15],["w","p",34]]
    print(list_of_DVDsuppliers)
    print
    ''' printing the available DVDstocks,supplier's details '''
    print("This is your current list of stock")
    print("Supplier Name\t\tSoftwear Name\t\tAmount")
    print("----------------------------------------------------------------")
    for [name, softname, amount] in list_of_DVDsuppliers:
        print("{0:23} {1:23} {2:5}".format(name, softname, amount))
        print("----------------------------------------------------------------")
    print("")

dvdprintsoftlist()
Sign up to request clarification or add additional context in comments.

5 Comments

this error appered Traceback (most recent call last): File "C:\Users\AAA\Desktop\python almost done\test of module useage.py", line 11, in <module> import modulenew File "C:\Users\AAA\Desktop\python almost done\modulenew.py", line 263 print "{0:23} {1:23} {2:5}".format(name, softname, amount) ^ SyntaxError: invalid syntax
Try this code in a new file. And see how it works, then try to add it to your code. PS: If it's a SyntaxError you may be writting something incorrect there
I don't understand, but if you are using python 3.x then make sure your print statement is print("{0:23} {1:23} {2:5}".format(name, softname, amount)) Note the ()
it works thanks a lot lot christian and could you help me with this quations also stackoverflow.com/questions/19556927/…
0

Try using the padding bits between the % and s while printing

you can use them like...

print('%-20s%-20s%-20s'%('supplier name','software name','amount'))  

-for left padding and +ve(default) for right padding

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.