【问题标题】:Formatting Text in a Table in Python在 Python 中格式化表格中的文本
【发布时间】:2013-04-13 04:16:21
【问题描述】:

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

我写了一个屏幕刮板从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()

试过 print ({0},{1},{2}.format (namesList, capital, price[i])),各种类型的 {:

从我的代码可以看出,我对编程很陌生,所以如果在这段代码中有更好的方法来做任何事情,我很乐意听取更正、建议和建议。

【问题讨论】:

    标签: python python-3.x web-scraping beautifulsoup tabular


    【解决方案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))
    

    【讨论】:

    • 效果很好,谢谢!我所做的唯一更改是在 names_width 和 stock_width 中都添加了 +2 以将其隔开一点。同样在 Python 3.3.1 中,我必须在花括号周围添加撇号以使其具有正确的语法。
    猜你喜欢
    • 2015-12-05
    • 2013-08-16
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    相关资源
    最近更新 更多