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.

atomicinf
  • 3,596
  • 19
  • 17
user2912389
  • 357
  • 3
  • 10
  • 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("") – user2912389 Oct 24 '13 at 02:54
  • 1
    Have you found http://stackoverflow.com/questions/9535954/python-printing-lists-as-tabular-data? – atomicinf Oct 24 '13 at 03:24
  • its nt working its python 3 – user2912389 Oct 24 '13 at 03:44

2 Answers2

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()
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • this error appered Traceback (most recent call last): File "C:\Users\AAA\Desktop\python almost done\test of module useage.py", line 11, in 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 – user2912389 Oct 24 '13 at 03:53
  • 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 – Christian Tapia Oct 24 '13 at 03:59
  • 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 () – Christian Tapia Oct 24 '13 at 04:15
  • it works thanks a lot lot christian and could you help me with this quations also http://stackoverflow.com/questions/19556927/create-a-list-from-a-csv-file-and-edit-and-save-back-data-in-csv-using-python – user2912389 Oct 24 '13 at 04:37
  • http://stackoverflow.com/questions/19555816/why-does-this-python-input-validation-fail – user2912389 Oct 24 '13 at 04:38
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

Sravan K Ghantasala
  • 1,058
  • 8
  • 14