0

だから私はこれをしました:

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')

ただし、結果のリストは次のとおりです。
'26.94']、['2011-10-07'、'26.34'、'26.51'、'26.20'、'26.25']、['2011-10-06'、'25.90'、'26.40'、'25.70 ', '26.34']]

['Date', 'Open', 'High', 'Low', 'Close'], ['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']

どうにかして二重リストをなくすことはできますか?

4

3 に答える 3

1

これはあなたが探しているものですか?

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)。要するに、出力する前に各行を最初の 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
于 2013-10-28T21:06:40.900 に答える
1

これにより、マイケルのソリューションよりも「自動化されていない」にもかかわらず、外観を簡単にカスタマイズできます。

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
于 2013-10-28T21:25:11.187 に答える
0

出力をきれいに見せたいだけなら、これを行う方法は本当にたくさんあります.2つの回答が指摘しているように、それは非常に簡単に行うことができます. 一般性だけが必要な場合は、必要なものすべてとしてコードを使用する必要があります

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の書式設定ははるかに優れたベスト プラクティス スタイルですが

于 2013-10-28T21:55:01.480 に答える