文本字符串到列表中

时间:2014-03-30 16:02:12

标签: python python-2.7

我开始使用Requests库了。

import requests

ticker='ibm'
startMonth='1'
startDate='1'
startYear='2013'

nowMonth='3'
nowDate='1'
nowYear='2014'

test='http://ichart.finance.yahoo.com/table.csv?s=' + ticker + '&a=' + startMonth + "&b=" + startDate + "&c=" + startYear + "&d=" + nowMonth + "e=" + nowDate + "&f=" + nowYear + "&g=d"

r=requests.get(test)
data=r.text

如何获取文本并转换为7个单独的列表?我不需要第一行

Date,Open,High,Low,Close,Volume,Adj Close
2014-03-28,189.94,192.62,189.11,190.45,5193700,190.45
2014-03-27,191.91,192.67,189.32,189.83,6767700,189.83
2014-03-26,194.98,195.63,191.96,192.62,6851700,192.62

我希望它看起来像这样

Date = [2014-03-28,2014-03-27,2014-03-26]
Open = [189.94,191.91,194.98]
High = ...
...

4 个答案:

答案 0 :(得分:4)

您可以使用zip(*data)转置一系列序列。

示例:

input = """Date,Open,High,Low,Close,Volume,Adj Close
2014-03-28,189.94,192.62,189.11,190.45,5193700,190.45
2014-03-27,191.91,192.67,189.32,189.83,6767700,189.83
2014-03-26,194.98,195.63,191.96,192.62,6851700,192.62"""

# split by newline, then each element by ','
data = [v.split(',') for v in input.rstrip().split('\n')]

# transpose the data and wrap it in a nice dict
result = {e[0]: e[1:] for e in zip(*data)}

# just for a pretty output
import pprint
pprint.pprint(result)

<强>输出:

{'Adj Close': ('190.45', '189.83', '192.62'),
 'Close': ('190.45', '189.83', '192.62'),
 'Date': ('2014-03-28', '2014-03-27', '2014-03-26'),
 'High': ('192.62', '192.67', '195.63'),
 'Low': ('189.11', '189.32', '191.96'),
 'Open': ('189.94', '191.91', '194.98'),
 'Volume': ('5193700', '6767700', '6851700')}

答案 1 :(得分:1)

headers = data.split('\n')[0].split(',')

for h in headers:
    master_list[h] = []

for line in data.split('\n')[1:]:
    items = line.split(',')
    for h, i in zip(headers,items):
        master_list[h].append(i)


master_list
{'Adj Close': ['190.45', '189.83', '192.62'],
 'Close': ['190.45', '189.83', '192.62'],
 'Date': ['2014-03-28', '2014-03-27', '2014-03-26'],
 'High': ['192.62', '192.67', '195.63'],
 'Low': ['189.11', '189.32', '191.96'],
 'Open': ['189.94', '191.91', '194.98'],
 'Volume': ['5193700', '6767700', '6851700']}

答案 2 :(得分:1)

您正在获取.csv文件,因此您可以使用csv模块阅读它:

import csv

sample = """Date,Open,High,Low,Close,Volume,Adj Close
2014-03-28,189.94,192.62,189.11,190.45,5193700,190.45
2014-03-27,191.91,192.67,189.32,189.83,6767700,189.83
2014-03-26,194.98,195.63,191.96,192.62,6851700,192.62"""

the_date = []
open_price = []

for row in csv.DictReader(sample.split('\n')):
    the_date.append(row['Date'])
    open_price.append(row['Open'])

print("Date = {}".format(the_date))
print("Open = {}".format(open_price))

答案 3 :(得分:1)

社区维基,因为这不是问题的直接答案,而是建议另一种解决同一问题的方法。如果您要处理OHLC数据,您应该查看pandas库:

>>> import pandas.io.data as web
>>> df = web.get_data_yahoo("IBM", "2013/1/1", "2014/3/1")
>>> df.head()
              Open    High     Low   Close   Volume  Adj Close
Date                                                          
2013-01-02  194.09  196.35  193.80  196.35  4234100     191.56
2013-01-03  195.67  196.29  194.44  195.27  3644700     190.51
2013-01-04  194.19  194.46  192.78  193.99  3380200     189.26
2013-01-07  193.40  193.78  192.34  193.14  2862300     188.43
2013-01-08  192.92  193.30  191.60  192.87  3026900     188.17

[5 rows x 6 columns]
>>> df["Volume"]
Date
2013-01-02     4234100
2013-01-03     3644700
2013-01-04     3380200
...
2014-02-27    3827800
2014-02-28    4667900
Name: Volume, Length: 285
>>> df["Volume"].max()
22368900