Formatting Text in a Table in Python

后端 未结 1 992
北荒
北荒 2020-12-12 05:02

I\'m having issues creating a table that is dynamic to adjust to various results.

I\'ve written a screen scraper to pull stocks from http://finance.yahoo.com and pri

相关标签:
1条回答
  • 2020-12-12 05:09

    You want to set the width based on the longest item in the column.

    In Python, you use max to find the largest of some group of things. So, outside the loop, you can do:

    names_width = max(len(name) for name in namesList)
    stock_width = max(len(stock) for stock in stockList)
    

    Then, format each line like you said you tried:

    print({0:{3}}  {1:{4}}  {2}.format(namesList[i],
                                       capital,
                                       price[i],
                                       names_width,
                                       stock_width))
    
    0 讨论(0)
提交回复
热议问题