小编典典

在Python中格式化表格中的文本

python

我在创建动态调整表格以适应各种结果时遇到问题。

我编写了一个屏幕抓取器,以从http://finance.yahoo.com提取股票并打印公司名称,符号和当前股价。

但是输出看起来像这样:

 Microsoft Corporation MSFT 29.76

 Apple Inc. AAPL 396.77

 SPDR S&P 500 SPY 155.25

 Google Inc. GOOG 787.76

我希望它看起来像

Microsoft Corporation        MSFT      29.76

Apple Inc.                   AAPL      396.77

SPDR S&P 500                 SPY       155.25

Google Inc.                  GOOG      787.76

我昨天刚开始使用Python,并且正在使用3.3.1

我当前的代码如下:

import re
import urllib.request
import cgi
from bs4 import BeautifulSoup

price = [0,0,0,0]
namesList = ["string1", "string2", "string3", "string4"]
stocksList = ["msft","aapl","spy","goog"]

def HTML():
    i = 0
    while i < len(stocksList):
        htmlPull = urllib.request.urlopen("http://finance.yahoo.com/q?s="+stocksList[i]+"&ql=1")
        htmlPull = htmlPull.read().decode('utf-8')
        regex = '<span id="yfs_l84_'+stocksList[i]+'">(.+?)</span>'
        pattern = re.compile(regex)
        price[i] = re.findall(pattern,htmlPull)
        htmlParse = BeautifulSoup(htmlPull)
        title = htmlParse.title.contents
        namesList[i] = title        
        i+=1

formatPrice(price)
formatStock(namesList)
formatOutput(namesList, stocksList, price)

def formatPrice(price):
    k=0
    while k < len(price):
        cleaner = str(price[k])
        cleaner = cleaner.replace("[","")
        cleaner = cleaner.replace("]","")
        cleaner = cleaner.replace("'","")
        price[k] = float(cleaner)
        k+=1

def formatStock(namesList):
    k = 0
    while k <len(namesList):
        capital = stocksList[k]
        capital = capital.upper()
        cleaner = str(namesList[k])
        cleaner = cleaner.replace("Summary for ", "")
        cleaner = cleaner.replace(":"," ")
        cleaner = cleaner.replace("- Yahoo! Finance'","")
        cleaner = cleaner.replace("['","")
        cleaner = cleaner.replace("]","")
        cleaner = cleaner.replace(";","")
        cleaner = cleaner.replace(capital, "")
        namesList[k] = cleaner;
        k+=1

    def formatOutput(namesList, stocksList, price):
        i = 0
        while i < len(price):
        capital = stocksList[i]
        capital = capital.upper()
        print(namesList[i],capital, price[i])
        print("")
        i+=1
HTML()

尝试打印({0},{1},{2} .format(namesList,capital,price
[i])),各种类型的{:<16}变体等。它似乎只影响一行,而我正在尝试让它根据列,表或可能需要填充文本和空白的一定数量的空间进行思考。我不确定这里到底是什么解决方案,所以我问大家:)

正如您可以从我的代码中看出的那样,我是编程的新手,因此,如果有一种更好的方法可以在此代码中执行任何操作,我将很乐意听取更正,建议和建议。


阅读 134

收藏
2020-12-20

共1个答案

小编典典

您要基于列中最长的项目设置宽度。

在Python中,您max用来查找某些事物中最大的事物。因此,在循环之外,您可以执行以下操作:

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

然后,按照您说过的方式格式化每一行:

print({0:{3}}  {1:{4}}  {2}.format(namesList[i],
                                   capital,
                                   price[i],
                                   names_width,
                                   stock_width))
2020-12-20