首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python雅虎财务格式

python雅虎财务格式
EN

Stack Overflow用户
提问于 2013-10-28 20:41:15
回答 3查看 938关注 0票数 0

所以我做了这个:

代码语言:javascript
复制
def get_quotes(ticker:str, start_date:datetime.date, end_date:datetime.date)->list:
'''Downloads the quotes from Yahoo finance'''


start_month = str(start_date.month-1)
start_day   = str(start_date.day)
start_year  = str(start_date.year)

end_month   = str(end_date.month-1)
end_day     = str(end_date.day)
end_year    = str(end_date.year)

try:
    list = []
    quote = 'http://ichart.yahoo.com/table.csv?s='+ticker+'&a'+start_month+'&b='+start_day+"&c="+start_year+'&d='+end_month+'&e='+ end_day +'&f='+end_year+'&g=d'
    response = urllib.request.urlopen(quote) 
    data = response.read()
    string_data = data.decode(encoding='utf-8')
    lines = string_data.splitlines()
    for x in lines:
        data = [y for y in x.split(',')]
        list.append(data[0:5])
    return list

except URLError:
    print('Page not found! Please enter a valid ticker')

但最终的结果是:[“日期”、“开放”、“高”、“低”、“关闭”、[“2011-10-10”、“26.58”、“26.97”、“26.47”,

代码语言:javascript
复制
 '26.94'], ['2011-10-07', '26.34', '26.51', '26.20', '26.25'], ['2011-10-06', '25.90',      '26.40', '25.70', '26.34']]

“日期”、“开放”、“高”、“低”、“关闭”、“2011-10-10”、“26.58”、“26.97”、“26.47”、“26.94”、“2011-10-07”、“26.34”、“26.51”、“26.20”、“26.25”、“2011-10-06”、“25.90”,“26.40”、“25.70”、“26.34”

我能以某种方式消除双重名单吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-28 21:06:40

这就是你要找的吗?

代码语言:javascript
复制
rows = ['Date,Open,High,Low,Close,Volume,Adj Close', '2012-11-30,691.31,699.22,685.69,698.37,3163600,698.37', '2012-11-29,687.78,693.90,682.00,691.89,2776500,691.89','2012-11-28,668.01,684.91,663.89,683.67,3042000,683.67', '2012-11-27,660.17,675.00,658.00,670.71,2508700,670.71']

def format_rows(rows, gap):
    split_rows = [row.split(',') for row in rows]
    # Splits each row up, by comma
    column_lengths = [max(col_len) for col_len in zip(*[map(len, row) for row in split_rows])]
    # Finds the maximum size of each column

    for row in split_rows:
        col_lengths = zip(row, column_lengths)
        print ''.join(col.ljust(col_length + gap, ' ') for (col, col_length) in col_lengths)
        # Prints out the data, making sure there's a minimum of "gap" spaces 
        # between each column

执行format_rows(rows, 4)将导致打印下表,每列之间的间隔为4个空格:

代码语言:javascript
复制
Date          Open      High      Low       Close     Volume     Adj Close
2012-11-30    691.31    699.22    685.69    698.37    3163600    698.37
2012-11-29    687.78    693.90    682.00    691.89    2776500    691.89
2012-11-28    668.01    684.91    663.89    683.67    3042000    683.67
2012-11-27    660.17    675.00    658.00    670.71    2508700    670.71

您可以修改代码,这样它就可以通过以下操作返回一个字符串:

代码语言:javascript
复制
def format_rows(rows, gap):
    split_rows = [row.split(',') for row in rows]
    # Splits each row up, by comma
    column_lengths = [max(col_len) for col_len in zip(*[map(len, row) for row in split_rows])]
    # Finds the maximum size of each column

    output = []
    for row in split_rows:
        col_lengths = zip(row, column_lengths)
        output.append(''.join(col.ljust(col_length + gap, ' ') for (col, col_length) in col_lengths))
    return '\n'.join(output)

编辑:

如果只想包含第一个n行,可以使用下面的函数并调用format_rows(rows, 4, 5)。本质上,我们在打印前将每一行截断为前五行。

代码语言:javascript
复制
def format_rows(rows, gap, limit):
    split_rows = [row.split(',') for row in rows]
    # Splits each row up, by comma
    column_lengths = [max(col_len) for col_len in zip(*[map(len, row) for row in split_rows])]
    # Finds the maximum size of each column

    for row in split_rows:
        col_lengths = zip(row, column_lengths)[:limit]
        # Prints out only the first `limit` columns

        print ''.join(col.ljust(col_length + gap, ' ') for (col, col_length) in col_lengths)
        # Prints out the data, making sure there's a minimum of "gap" spaces 
        # between each column
票数 1
EN

Stack Overflow用户

发布于 2013-10-28 21:25:11

使用它,您可以轻松地自定义外观,尽管它比Michael的解决方案“不那么自动”:

代码语言:javascript
复制
lines = [x.split(',') for x in a]
for line in lines:
    print "{0[0]:<10} {0[1]:<6} {0[2]:<6} {0[3]:<6} {0[4]:<6} {0[5]:<7} {0[6]:<6}".format(line)

结果:

代码语言:javascript
复制
Date       Open   High   Low    Close  Volume  Adj Close
2012-11-30 691.31 699.22 685.69 698.37 3163600 698.37
2012-11-29 687.78 693.90 682.00 691.89 2776500 691.89
2012-11-28 668.01 684.91 663.89 683.67 3042000 683.67

想要显示以第一个列为中心,所有其他列都对齐,在最后一个列中有一个很大的空白,而忽略开列?只是对格式字符串做了一个小小的更改:

"{0[0]:^10} {0[2]:>6} {0[3]:>6} {0[4]:>6} {0[5]:>7} {0[6]:>12}"

(见格式字符串语法)

你得到的是:

代码语言:javascript
复制
   Date      High    Low  Close  Volume    Adj Close
2012-11-30 699.22 685.69 698.37 3163600       698.37
2012-11-29 693.90 682.00 691.89 2776500       691.89
2012-11-28 684.91 663.89 683.67 3042000       683.67
票数 1
EN

Stack Overflow用户

发布于 2013-10-28 21:55:01

如果你只想让你的输出看起来很漂亮,那么有很多方法可以做到这一点,就像这两个回应所指出的那样,你可以很容易地做到这一点。如果你只是想要一个一般性,那么你的代码作为它所需要的一切,你只需要

代码语言:javascript
复制
for x in lines:
    print x

但是,如果要生成行列表,则必须执行以下操作:

代码语言:javascript
复制
lst = []

for x in lines:
    data = [y for y in x.split(',')]
    lst.append(data)

for x in lst:
    print x

['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close']
['2012-11-30', '691.31', '699.22', '685.69', '698.37', '3163600', '698.37']
['2012-11-29', '687.78', '693.90', '682.00', '691.89', '2776500', '691.89']
['2012-11-28', '668.01', '684.91', '663.89', '683.67', '3042000', '683.67']
['2012-11-27', '660.17', '675.00', '658.00', '670.71', '2508700', '670.71']
['2012-11-26', '666.44', '667.00', '659.02', '661.15', '2204600', '661.15']
['2012-11-23', '669.97', '670.00', '666.10', '667.97', '922500', '667.97']

但是对于简单的输出,您可以去掉日期,打开行并执行以下操作:

代码语言:javascript
复制
print('Date         Open     High     Low      Closee    Volume     Adj Close')
del lines[0]
for x in lines:
    data = [y for y in x.split(',')]
    print("{0}   {1}   {2}   {3}   {4}   {5}    {6}".format(*data))

Date         Open     High     Low      Close    Volume     Adj Close
2012-11-30   691.31   699.22   685.69   698.37   3163600    698.37
2012-11-29   687.78   693.90   682.00   691.89   2776500    691.89
2012-11-28   668.01   684.91   663.89   683.67   3042000    683.67
2012-11-27   660.17   675.00   658.00   670.71   2508700    670.71
2012-11-26   666.44   667.00   659.02   661.15   2204600    661.15

希望这能有所帮助。尽管LeartS的格式要好得多,但最佳实践风格却要好得多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19644702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档