[英]python yahoo finance format
所以我這樣做了:
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',
'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 0.34' ]
我能以某種方式消除雙重名單嗎?
這是你在找什么?
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個空格:
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
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)
。 實質上,我們在打印之前將每一行截斷為前五行。
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
有了這個,您可以輕松自定義外觀,即使它比邁克爾的解決方案“更不自動”:
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)
結果:
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}"
(參見格式字符串語法 )
你得到:
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
如果您只是希望輸出看起來很漂亮,那么有很多方法可以做到這一點,因為兩個響應已經指出您可以很容易地做到這一點。 如果你只是想要一般性,那么你的代碼就像它需要的一切你只需要
for x in lines:
print x
但是,如果要生成行列表,則必須執行以下操作:
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']
但是對於簡單漂亮的輸出你可以讓你擺脫日期,打開線並執行此操作:
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的格式遠遠好於最好的練習風格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.